Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

script for loadStoryblokBridge is injected twice #943

Open
1 task done
oddcelot opened this issue Oct 1, 2024 · 2 comments
Open
1 task done

script for loadStoryblokBridge is injected twice #943

oddcelot opened this issue Oct 1, 2024 · 2 comments
Labels
pending-author [Issue] Awaiting further information or action from the issue author pending-triage [Issue] Ticket is pending to be prioritised

Comments

@oddcelot
Copy link

oddcelot commented Oct 1, 2024

Describe the issue you're facing

I was trying out the storyblok integration for astro (with v5 beta) as was noticing this warning in the console:

Caution

Uncaught SyntaxError: Identifier 'loadStoryblokBridge' has already been declared (at page.js:14:24)

During dev this not an issue, but when building an error gets also thrown:

Caution

astro:scripts/page.js (14:23): Identifier "loadStoryblokBridge" has already been declared
file: astro:scripts/page.js:14:23

12:             
13: 
14:               import { loadStoryblokBridge, syncContentUpdate } from "@storyblok/astro";
                          ^
15:               loadStoryblokBridge().then(() => {
16:                 const { StoryblokBridge } = window;

 Hint:
   Browser APIs are not available on the server.
   
   If the code is in a framework component, try to access these objects after rendering using lifecycle methods or use a `client:only` directive to make the component exclusively run on the client.
   
   See https://docs.astro.build/en/guides/troubleshooting/#document-or-window-is-not-defined for more information.
   

It seems like this is related to this section:

injectScript(
"page",
`
import { loadStoryblokBridge } from "@storyblok/astro";
loadStoryblokBridge().then(() => {
const { StoryblokBridge, location } = window;
${initBridge}
storyblokInstance.on(["published", "change"], (event) => {
if (!event.slugChanged) {
location.reload(true);
}
});
});
`
);
}
// This is only enabled if experimentalLivePreview feature is on
if (resolvedOptions.livePreview) {
injectScript(
"page",
`
import { loadStoryblokBridge, handleStoryblokMessage } from "@storyblok/astro";
import { bridgeOptions } from "virtual:storyblok-bridge";
console.info("The Storyblok Astro live preview feature is currently in an experimental phase, and its API is subject to change in the future.")
loadStoryblokBridge().then(() => {
const { StoryblokBridge, location } = window;
if(bridgeOptions){
const storyblokInstance = new StoryblokBridge(bridgeOptions);
storyblokInstance.on(["published", "change", "input"], handleStoryblokMessage);
};
});
`

But looking at the config it seems fine and in line with the flow control here, so no idea why the script is injected both times, unless the resolved config is somehow different.

Reproduction

https://github.com/oddcelot/astro-storyblok

Steps to reproduce

No response

System Info

"@storyblok/astro": "5.1.0-alpha.4",
"astro": "5.0.0-beta.3"

Used Package Manager

pnpm

Error logs (Optional)

No response

Validations

@oddcelot oddcelot added pending-author [Issue] Awaiting further information or action from the issue author pending-triage [Issue] Ticket is pending to be prioritised labels Oct 1, 2024
@oddcelot
Copy link
Author

oddcelot commented Oct 1, 2024

So i did try and patch this via pnpm patch and removing the additonal import seems to resolve the issues for dev and build 🎉
oddcelot/astro-storyblok@73855c8

@manuelschroederdev
Copy link
Contributor

Hey @oddcelot, thank you very much for opening this issue and looking into it. @dipankarmaikap and I currently working on a new major release of @storyblok/astro and will address this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending-author [Issue] Awaiting further information or action from the issue author pending-triage [Issue] Ticket is pending to be prioritised
Projects
None yet
Development

No branches or pull requests

2 participants