8000 fix: correctly invalidate static analysis cache of child nodes during dev by eltigerchino · Pull Request #13793 · sveltejs/kit · GitHub
[go: up one dir, main page]

Skip to content

fix: correctly invalidate static analysis cache of child nodes during dev #13793

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add correct test
  • Loading branch information
eltigerchino committed May 16, 2025
commit d44e2de174d9b0809dde425de7bb124de30b3ecc
2 changes: 1 addition & 1 deletion .changeset/big-eels-watch.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
'@sveltejs/kit': patch
---

fix: correctly invalidate static analysis cache when modifying a universal `+page`/`+layout` file during dev
fix: correctly invalidate static analysis cache of child nodes when modifying a universal `+layout` file during dev
15 changes: 2 additions & 13 deletions packages/kit/src/exports/vite/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,13 +353,14 @@ export async function dev(vite, vite_config, svelte_config) {

// Debounce add/unlink events because in case of folder deletion or moves
// they fire in rapid succession, causing needless invocations.
// These watchers only run for routes, param matchers, and client hooks.
watch('add', () => debounce(update_manifest));
watch('unlink', () => debounce(update_manifest));
watch('change', (file) => {
// Don't run for a single file if the whole manifest is about to get updated
if (timeout || restarting) return;

if (is_universal_file(file)) {
if (/\+(page|layout).*$/.test(file)) {
invalidate_page_options(path.relative(cwd, file));
}

Expand Down Expand Up @@ -391,10 +392,6 @@ export async function dev(vite, vite_config, svelte_config) {
});

vite.watcher.on('change', async (file) => {
if (is_universal_file(file)) {
invalidate_page_options(path.relative(cwd, file));
}

// changing the svelte config requires restarting the dev server
// the config is only read on start and passed on to vite-plugin-svelte
// which needs up-to-date values to operate correctly
Expand Down Expand Up @@ -654,11 +651,3 @@ function has_correct_case(file, assets) {

return false;
}

/**
* @param {string} file
* @returns {boolean}
*/
function is_universal_file(file) {
return /\+(page|layout).*$/.test(file);
}
1 change: 1 addition & 0 deletions packages/kit/src/exports/vite/static_analysis/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ export function create_static_analyser(resolve) {
* @returns {void}
*/
const invalidate_page_options = (file) => {
// TODO: this needs to invalidate nodes lower in the branch too
static_exports.delete(file);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ssr = false;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
document.body.style.backgroundColor = 'red';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>hello world</p>
25 changes: 24 additions & 1 deletion packages/kit/test/apps/writes/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,40 @@ test.describe('Filesystem updates', () => {
}

await page.waitForTimeout(500); // this is the rare time we actually need waitForTimeout; we have no visibility into whether the module graph has been invalidated
// a reload is required because Vite HMR doesn't trigger if the page has never loaded successfully
await page.reload();
expect(await get_computed_style('body', 'background-color')).toBe('rgb(255, 0, 0)');

try {
fs.writeFileSync(file, contents.replace(/export const ssr = .*;/, 'export const ssr = !1;'));
await page.waitForTimeout(500); // this is the rare time we actually need waitForTimeout; we have no visibility into whether the module graph has been invalidated
await page.goto('/universal', { wait_for_started: false });
expect(await get_computed_style('body', 'background-color')).not.toBe('rgb(255, 0, 0)');
await expect(page.locator('h1')).toHaveText('Internal Error');
} finally {
fs.writeFileSync(file, contents.replace(/\\nexport const ssr = false;\\n/, ''));
}
});

test('Universal node is updated when parent page options change', async ({
page,
javaScriptEnabled,
get_computed_style
}) => {
test.skip(!process.env.DEV || !javaScriptEnabled);

const file = fileURLToPath(new URL('../src/routes/universal/parent-changed/+layout.js', import.meta.url));
const contents = fs.readFileSync(file, 'utf-8');

try {
await page.goto('/universal/parent-changed');
expect(await get_computed_style('body', 'background-color')).toBe('rgb(255, 0, 0)');

fs.writeFileSync(file, contents.replace(/export const ssr = false;/, ''));
await page.waitForTimeout(500); // this is the rare time we actually need waitForTimeout; we have no visibility into whether the module graph has been invalidated
expect(await get_computed_style('body', 'background-color')).not.toBe('rgb(255, 0, 0)');
await expect(page.locator('h1')).toHaveText('Internal Error');
} finally {
fs.writeFileSync(file, contents);
}
});
});
Loading
0