Please specify what version of the library you are using: [ 2.0.4 ]
Please specify what version(s) of SharePoint you are targeting: [ Online ]
If you are not using the latest release, please update and see if the issue is resolved before submitting an issue.
Following code should setup context properly even on pages inside SitePages
sp.setup({
spfxContext: this.context
});
The setup sometimes works and sometimes not
I recorded a video: https://youtu.be/UwWGLWp3Nqo
Description of the video:
I used a setup like here:
protected onInit() : Promise<void>
{
sp.setup({
spfxContext: this.context
});
return super.onInit();
}
I have a feeling that you're calling sp.setup in the wrong order. onInit is async it's better to follow the samples more strictly. this.context should be passed to sp.setup after super.onInit is already resolved otherwise part of the context might not be populated yet.
Can you also verify console.log(this.context.pageContext.web.absoluteUrl) contains web absolute URL at the time you're calling sp.setup and that the PnPjs is not called before sp.setup? But it's better to establish the context in the same way as the docs suggest to avoid issues that are actually not related to the library and mount any code calling API after the init is resolved.
You are absolutely right. I am not sure know if I missed this .then or used some other example as I can remember that I saw also this version... Nvm thank you very much. Now it is working fine.
Glad you've figured out and now it works for you. Happy coding!