-
Notifications
You must be signed in to change notification settings - Fork 92
fix: skip script downloads for unused components #884
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
Open
harlan-zw
wants to merge
6
commits into
main
Choose a base branch
from
fix/unused-widget-script-download
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4c1dfd9
fix: skip script downloads for unused components
harlan-zw aaaab24
fix(transform): drop empty integrity and crossorigin when deferred bu…
harlan-zw e8b907a
test(e2e): cover deferred used-component bundling with integrity + cr…
harlan-zw 50104d2
perf(transform): overlap deferred component bundle downloads in rende…
harlan-zw 1ed7610
fix(transform): resolve deferred placeholders at emit time
harlan-zw ece09f1
fix(transform): keep applying placeholder replacements across watch r…
harlan-zw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { createResolver } from '@nuxt/kit' | ||
| import { $fetch, setup } from '@nuxt/test-utils/e2e' | ||
| import { describe, expect, it } from 'vitest' | ||
|
|
||
| const { resolve } = createResolver(import.meta.url) | ||
|
|
||
| await setup({ | ||
| rootDir: resolve('../fixtures/issue-882'), | ||
| build: true, | ||
| browser: false, | ||
| }) | ||
|
|
||
| describe('unused script widgets', () => { | ||
| it('builds without downloading their scripts', async () => { | ||
| await expect($fetch('/')).resolves.toContain('Nuxt Scripts') | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { createHash } from 'node:crypto' | ||
| import { createResolver } from '@nuxt/kit' | ||
| import { $fetch, setup } from '@nuxt/test-utils/e2e' | ||
| import { describe, expect, it } from 'vitest' | ||
|
|
||
| const { resolve } = createResolver(import.meta.url) | ||
|
|
||
| await setup({ | ||
| rootDir: resolve('../fixtures/issue-882-used'), | ||
| build: true, | ||
| browser: false, | ||
| }) | ||
|
|
||
| const ABS_CHUNK_RE = /\/_nuxt\/[\w-]+\.js/g | ||
| const REL_CHUNK_RE = /\.\/([\w-]+\.js)/g | ||
|
|
||
| /** | ||
| * Walk the built client module graph over HTTP, starting from the entry chunk | ||
| * referenced by the served page's import map, until we find the chunk that | ||
| * carries the rewritten `useScript*` call for the bundled widget. Chunks are | ||
| * connected by both absolute (`/_nuxt/x.js`) and relative (`./x.js`) specifiers. | ||
| */ | ||
| async function findWidgetChunk(entryUrl: string, marker: string): Promise<string[]> { | ||
| const queue = [entryUrl] | ||
| const visited = new Set<string>() | ||
| const hits: string[] = [] | ||
| let guard = 0 | ||
| while (queue.length && guard < 200) { | ||
| guard++ | ||
| const url = queue.shift()! | ||
| if (visited.has(url)) | ||
| continue | ||
| visited.add(url) | ||
| const code = await $fetch(url) | ||
| const refs = new Set<string>() | ||
| for (const ref of code.match(ABS_CHUNK_RE) || []) | ||
| refs.add(ref) | ||
| for (const ref of code.match(REL_CHUNK_RE) || []) | ||
| refs.add(`/_nuxt/${ref.slice(2)}`) | ||
| for (const ref of refs) { | ||
| if (!visited.has(ref)) | ||
| queue.push(ref) | ||
| } | ||
| if (code.includes(marker)) | ||
| hits.push(code) | ||
| } | ||
| return hits | ||
| } | ||
|
|
||
| describe('used script widget (deferred component path)', () => { | ||
| it('bundles the used widget script and keeps integrity + crossorigin through renderStart', async () => { | ||
| const html = await $fetch('/') | ||
| expect(html).toContain('Nuxt Scripts') | ||
|
|
||
| // The deferred used-component path must resolve the placeholder to the | ||
| // content-addressed public bundle URL rather than the remote src. | ||
| const assetUrl = html.match(/\/_scripts\/assets\/[a-f0-9]{16}\.js/)?.[0] | ||
| expect(assetUrl, 'expected a bundled /_scripts/assets/<hash>.js reference in the served page').toBeTruthy() | ||
|
|
||
| // The integrity hash computed on the served bundle must match the hash the | ||
| // deferred renderStart path baked into the page (the script preload link). | ||
| const assetBody = await $fetch(assetUrl!) | ||
| const expectedIntegrity = `sha384-${createHash('sha384').update(assetBody).digest('base64')}` | ||
| expect(html).toContain(`integrity="${expectedIntegrity}"`) | ||
|
|
||
| // The same src + integrity + crossorigin must survive into the built client | ||
| // chunk that drives the runtime script injection. | ||
| const entry = html.match(/"#entry":"(\/_nuxt\/[\w-]+\.js)"/)?.[1] | ||
| expect(entry, 'expected an entry chunk in the served page import map').toBeTruthy() | ||
| const widgetChunks = await findWidgetChunk(entry!, assetUrl!) | ||
| expect(widgetChunks.length, 'expected a built client chunk referencing the bundled asset').toBeGreaterThan(0) | ||
| const rewritten = widgetChunks.join('\n') | ||
| expect(rewritten).toContain(`integrity:\`${expectedIntegrity}\``) | ||
| expect(rewritten).toContain(`crossorigin:\`anonymous\``) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <template> | ||
| <main> | ||
| Nuxt Scripts | ||
| <ScriptCalendlyInlineWidget | ||
| url="https://calendly.com/example/30min" | ||
| trigger="onNuxtReady" | ||
| /> | ||
| </main> | ||
| </template> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.