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

Fix Windows dev script proxying #2052

Merged
merged 2 commits into from
Nov 30, 2021
Merged

Fix Windows dev script proxying #2052

merged 2 commits into from
Nov 30, 2021

Conversation

drwpow
Copy link
Member

@drwpow drwpow commented Nov 29, 2021

Changes

Fixes #2053. In dev, Windows was getting bad URLs for script proxying. This behavior only existed in dev, not build, so dev tests needed to be added that we didn’t have before.

Testing

Tests added that didn’t exist before.

Docs

No docs changes.

@changeset-bot
Copy link

changeset-bot bot commented Nov 29, 2021

⚠️ No Changeset found

Latest commit: 2ee36e0

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@netlify
Copy link

netlify bot commented Nov 29, 2021

✔️ Deploy Preview for astro-www ready!

🔨 Explore the source changes: 2ee36e0

🔍 Inspect the deploy log: https://app.netlify.com/sites/astro-www/deploys/61a577acaf48e10007b6ce74

😎 Browse the preview: https://deploy-preview-2052--astro-www.netlify.app

@netlify
Copy link

netlify bot commented Nov 29, 2021

✔️ Deploy Preview for astro-docs-2 ready!

🔨 Explore the source changes: 2ee36e0

🔍 Inspect the deploy log: https://app.netlify.com/sites/astro-docs-2/deploys/61a577ace559090007d92ba3

😎 Browse the preview: https://deploy-preview-2052--astro-docs-2.netlify.app

@github-actions github-actions bot added pkg: astro Related to the core `astro` package (scope) test labels Nov 29, 2021
@drwpow drwpow force-pushed the script-proxy-tests branch 2 times, most recently from 6c9e964 to 3f717b4 Compare November 30, 2021 00:59
@drwpow drwpow changed the title [wip] Add tests for script proxying [wip] Fix Windows dev script proxying Nov 30, 2021
@drwpow drwpow changed the title [wip] Fix Windows dev script proxying Fix Windows dev script proxying Nov 30, 2021
@@ -266,7 +266,8 @@ export async function render(renderers: Renderer[], mod: ComponentInstance, ssrO

// run transformIndexHtml() in dev to run Vite dev transformations
if (mode === 'development') {
html = await viteServer.transformIndexHtml(viteifyURL(filePath), html, pathname);
const relativeURL = filePath.href.replace(astroConfig.projectRoot.href, '/');
html = await viteServer.transformIndexHtml(relativeURL, html, pathname);
Copy link
Member Author

@drwpow drwpow Nov 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Big change 1: use project-relative URLs to generate the script proxies. Here’s the difference:

- <script type="module" src="/:/Users/astro/code/my-project/src/pages/index.astro?html&proxy=0.js">
+ <script type="module" src="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/index.astro?html&proxy=0.js">

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note the /:/ is because C:/ isn’t the start of a browser-compatible pathname

// mark the entrypoint as scanned to avoid an infinite loop
scanned.add(moduleName.id);
scanned.add(moduleName.url);
Copy link
Member Author

@drwpow drwpow Nov 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Big change 2: use url instead of id in Vite’s module graph. Here’s the difference between the two, according to Vite:

ID

  • Linux/Mac: /Users/astro/code/my-project/src/pages/index.astro ✅ valid in module graph & in browser
  • Windows: C:/Users/astro/code/my-project/src/pages/index.astro ❌ valid in module graph but not valid in browser (the original bug)

URL

  • Linux/Mac: /@fs/Users/astro/code/my-project/src/pages/index.astro ✅ valid in module graph & in browser
  • Windows: /@fs/C:/Users/astro/code/my-project/src/pages/index.astro ✅ valid in module graph & in browser

This PR uses url so that the same value can be placed in the browser and resolved.

*/
export function viteifyURL(filePath: URL): string {
return slash(fileURLToPath(filePath));
return `/@fs${filePath.pathname}`;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updates the viteifyURL function to use module graph url over id

if (match[1].includes('Research.js')) {
componentUrl = match[1];
break;
describe('dev', () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No build tests changed; only added “dev” tests

} else {
this.viteServer.moduleGraph.invalidateAll();
}
await this.viteServer.moduleGraph.invalidateAll();
Copy link
Member Author

@drwpow drwpow Nov 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last change: when something threw an error, we were doing too much work to only invalidate some modules, possibly missing many in the process. If something has erred, just invalidate everything 🤷🏻.

@drwpow drwpow marked this pull request as ready for review November 30, 2021 16:45
Copy link
Contributor

@tony-sull tony-sull left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! These fixes to handle Windows filenames and the test updates look great to me 👍

Added one note on a skipped test, but I'm not 100% sure on whether the test is useful or not and don't think that should block merging in these fixes

https://docs.astro.build/reference/api-reference/#importmeta`
);
});
// TODO: is this still a relevant test?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, it seems like this should still be a useful test?

Test #2 checks that data-reactroot isn't added to the test page's <Hello /> component since it isn't using client hydration. It looks like this test would be making sure that data-reactroot is still used on the hydrated <Research /> component

What I'm not actually sure on is whether data-reactroot is still used to flag hydrated react components. If yes, I think this and test #2 should be useful, if no I think both tests aren't helpful anymore (since test #2 would always get undefined regardless of how hydration was used)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this test has been skipped since the move to Vite/0.21. @natemoo-re what should this test look for now?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe data-reactroot is required for hydrated components still. If this test is broken it might be a symptom of a bug we need to fix. Happy to spin off into a separate PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. Yeah if that could be investigated separately (ideally testing dev and build) that would be good. This test needs to be updated, but I wasn’t sure exactly how (and is unrelated to this PR).

@drwpow drwpow merged commit 03cabc5 into main Nov 30, 2021
@drwpow drwpow deleted the script-proxy-tests branch November 30, 2021 18:54
drwpow added a commit that referenced this pull request Dec 1, 2021
@drwpow drwpow mentioned this pull request Dec 1, 2021
drwpow added a commit that referenced this pull request Dec 1, 2021
drwpow added a commit that referenced this pull request Dec 1, 2021
drwpow added a commit that referenced this pull request Dec 1, 2021
SiriousHunter pushed a commit to SiriousHunter/astro that referenced this pull request Feb 3, 2023
* Add tests for script proxying

* Fix Windows script proxying

withastro#2053
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 BUG: Vite inline script proxing broken on Windows
3 participants