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

Astro 3.0 dev server error when base path is set and React component loaded #8352

Closed
1 task
dallyh opened this issue Sep 1, 2023 · 2 comments · Fixed by #8428
Closed
1 task

Astro 3.0 dev server error when base path is set and React component loaded #8352

dallyh opened this issue Sep 1, 2023 · 2 comments · Fixed by #8428
Assignees
Labels
- P5: urgent Fix build-breaking bugs affecting most users, should be released ASAP (priority)

Comments

@dallyh
Copy link

dallyh commented Sep 1, 2023

Astro Info

Astro                    v3.0.5
Node                     v18.17.1
System                   Windows (x64)
Package Manager          npm
Output                   static
Adapter                  none
Integrations             @astrojs/react

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

When the base path is set in the Astro config, and an React component is used with the "client" directive, Astro fails to load the webpage only when using the dev server. After the webpage gets build everything works fine. This kind of neglectes any attempts to quickly develop and debug React components. This issue seems a little widespread, as some people on Discord confirmed that it is not only me.

Config is set like this:

// https://astro.build/config
export default defineConfig({
  integrations: [react()],
  base: "basepath",
});

The error which prevents load/render of the page is:

Failed to resolve import "/basepath/@react-refresh" from "astro:scripts/before-hydration.js". Does the file exist?

What's the expected result?

Site with React components and a base path set in a config should work in a dev server without any errors.

Link to Minimal Reproducible Example

https://stackblitz.com/github/dallyh/astro-react-refresh-issue

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Sep 1, 2023
@paoloose
Copy link

paoloose commented Sep 1, 2023

Relevant piece of code introduced in Astro 3.0: e07a4e5#diff-714fabdf53991de22f8549ac97613b5d49f9c6aaa096a57d8a29f220758978c4R66-R69

Plugin's side: https://github.com/vitejs/vite-plugin-react/blob/286360281992c425bf75cb0a18846f65fcdc5ef3/packages/plugin-react/src/fast-refresh.ts#L23-L29

Not replacing __BASE__ with the Astro config.base but with a forward slash / completely solved the problem for me, but I'm not sure it's the proper fix or why it works

        if (command === "dev") {
          const preamble = FAST_REFRESH_PREAMBLE.replace(
            `__BASE__`,
-            appendForwardSlash(config.base)
+           '/'
          );
          injectScript("before-hydration", preamble);
        }

@sasoria
Copy link
Contributor

sasoria commented Sep 2, 2023

I'm getting the same error as well. This is a temporary fix I'm using:

export default defineConfig({
  base: import.meta.env.DEV ? "" : "/base-path",
  ...
}); 

tordans added a commit to tordans/berlin-bikenetwork-monitoring that referenced this issue Sep 6, 2023
tordans added a commit to tordans/docs-1 that referenced this issue Sep 6, 2023
@delucis delucis added the - P5: urgent Fix build-breaking bugs affecting most users, should be released ASAP (priority) label Sep 6, 2023
@github-actions github-actions bot removed the needs triage Issue needs to be triaged label Sep 6, 2023
@matthewp matthewp self-assigned this Sep 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P5: urgent Fix build-breaking bugs affecting most users, should be released ASAP (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants