Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/fix-sync-root-types-and-metadata-cache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: retain root route types and metadata cache across sync runs
6 changes: 5 additions & 1 deletion packages/kit/src/core/sync/write_types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,15 @@ export function write_all_types(config, manifest_data, root) {
posixify(path.relative(root, config.files.routes))
);
const expected_directories = new Set(
manifest_data.routes.map((route) => path.posix.join(routes_dir, route.id))
manifest_data.routes.map((route) => {
const dir = path.posix.join(routes_dir, route.id);
return dir.endsWith('/') && dir !== '/' ? dir.slice(0, -1) : dir;
})
);

if (fs.existsSync(types_dir)) {
for (const file of walk(types_dir)) {
if (file === 'route_meta_data.json') continue;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Preserving the metadata cache causes subsequent syncs to leave layout route IDs and params stale when child pages are added or deleted without changing load files.

Fix on Vercel

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

With metadata now retained, moving a component-only route directory via files.routes leaves its generated $types.d.ts missing after sync.

Fix on Vercel

const dir = path.posix.dirname(file);
if (!expected_directories.has(dir)) {
fs.rmSync(path.join(types_dir, file), { force: true, recursive: true });
Expand Down
38 changes: 38 additions & 0 deletions packages/kit/src/core/sync/write_types/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,3 +402,41 @@ test('prunes stale entries from route_meta_data.json when routes are deleted', (
fs.rmSync(tmp, { force: true, recursive: true });
}
});

test('preserves root route $types.d.ts and route_meta_data.json on repeated sync runs', () => {
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'sveltekit-sync-cache-test-'));
try {
const config = validate_config({});
config.files.assets = path.resolve(cwd, 'static');
const routes_dir = path.resolve(tmp, 'src', 'routes');
fs.mkdirSync(routes_dir, { recursive: true });
fs.writeFileSync(path.join(routes_dir, '+page.svelte'), '<h1>Home</h1>');

config.files.params = path.resolve(tmp, 'src', 'params');
config.files.routes = routes_dir;
config.outDir = path.resolve(tmp, '.svelte-kit');

const manifest = create_manifest_data(config, tmp);

// First sync run: generates types and route_meta_data.json
write_all_types(config, manifest, tmp);

const root_types_file = path.join(config.outDir, 'types', 'src', 'routes', '$types.d.ts');
const meta_data_file = path.join(config.outDir, 'types', 'route_meta_data.json');

expect(fs.existsSync(root_types_file)).toBe(true);
expect(fs.existsSync(meta_data_file)).toBe(true);

// Append a marker comment to verify the root type file and cache are retained
fs.appendFileSync(root_types_file, '\n// marker-test-sync-cache');

// Second sync run: should not wipe root types or route_meta_data.json
write_all_types(config, manifest, tmp);

expect(fs.existsSync(root_types_file)).toBe(true);
expect(fs.existsSync(meta_data_file)).toBe(true);
expect(fs.readFileSync(root_types_file, 'utf-8')).toContain('// marker-test-sync-cache');
} finally {
fs.rmSync(tmp, { force: true, recursive: true });
}
});
Loading