Hi Team,
I am trying to update the SharePoint modern page. For the Very first time when ever the site is created, when i try to get the page object and just save and publish the page looses all the controls/sections and webparts on it.
Below is the code which i am using in my solution.
const mySP = sp.configure({
cache: "default"
}, this.props.webPartContext.pageContext.web.absoluteUrl);
const page: ClientSidePage = await ClientSidePage.fromFile(mySP.web.getFileByServerRelativeUrl(this.props.webPartContext.pageContext.web.serverRelativeUrl + "/" + WelcomePageURL["WelcomePage"]));
await page.save();
page.publish();
Also one more observation, when i edit and publish page manually by going to site then run the above code then nothing will be lost.
Could you please help me with with any suggestion/solution.
Thanks & Regards,
Pavan Nesargi.
I just tried the following code in the SPEditor (Chrome extension) console, in my SharePoint dev tenant.
I have an instance of "SharePoint starter kit" deployed and I tried to access the Personal.aspx, save it and publish. This worked splendid.
import { sp, ClientSidePage } from "@pnp/sp";
(async () => {
const file = sp.web.getFileByServerRelativeUrl('/sites/tmportal/SitePages/Personal.aspx');
const page = await ClientSidePage.fromFile(pageFile);
await page.save();
await page.publish();
})().then(_ => console.log('Yay')).catch(console.log);
@simonagren , thanks for the reply. Yes custom page its working as expected but he Landing page(Welcome page) which is Home.aspx, once the site is created if you try the above code with that page then all the WP will be removed form the page.
FYI

Only for the first time, if you Edit and publish(Manually/grammatically) and then again try the above things it works as expected.
@pavan2920 @patrick-rodgers this is a bug in SharePoint as far as I can tell.
It's not a PnPJS bug.
When you create a new teamsite and try to access the "welcome page" via rest, either via PnPJS or just "rest":
/_api/web/getFileByServerRelativeUrl('/sites/test2/sitepages/home.aspx')/listItemAllFields?$select=CanvasContent1,CommentsDisabled
You don't get the sections and the webparts included. So when you try to save and publish via rest, you save an empty page really.

ONLY after an Manual edit and publish of the page, the same restcall gets the sections etc.

Thanks @simonagren - there is some weirdness with the welcome page in sites.
@patrick-rodgers @patmill commented it here:
https://github.com/SharePoint/sp-dev-docs/issues/2527#issuecomment-421155147