Skip to content

Commit 1a2e432

Browse files
authored
expose event.routeId and page.routeId (#4345)
* expose event.routeKey - closes #3840 * change routeKey to routeId * rename routeKey to routeId, expose page.routeId * rename route.key -> route.id everywhere * adapter-node sure picked a weird time to stop typechecking * oops * Update .changeset/mean-crews-unite.md
1 parent 984ca0a commit 1a2e432

File tree

29 files changed

+254
-200
lines changed

29 files changed

+254
-200
lines changed

.changeset/mean-crews-unite.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
Expose `event.routeId` and `page.routeId`

packages/adapter-node/src/handler.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ const ssr = async (req, res) => {
4444
request = await getRequest(origin || get_origin(req.headers), req);
4545
} catch (err) {
4646
res.statusCode = err.status || 400;
47-
return res.end(err.reason || 'Invalid request body');
47+
res.end(err.reason || 'Invalid request body');
48+
return;
4849
}
4950

5051
if (address_header && !(address_header in req.headers)) {

packages/kit/src/core/dev/plugin.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { coalesce_to_error } from '../../utils/error.js';
1212
import { load_template } from '../config/index.js';
1313
import { sequence } from '../../hooks.js';
1414
import { posixify } from '../../utils/filesystem.js';
15-
import { parse_route_key } from '../../utils/routing.js';
15+
import { parse_route_id } from '../../utils/routing.js';
1616

1717
/**
1818
* @param {import('types').ValidatedConfig} config
@@ -105,12 +105,12 @@ export async function create_plugin(config, cwd) {
105105
};
106106
}),
107107
routes: manifest_data.routes.map((route) => {
108-
const { pattern, names, types } = parse_route_key(route.key);
108+
const { pattern, names, types } = parse_route_id(route.id);
109109

110110
if (route.type === 'page') {
111111
return {
112112
type: 'page',
113-
key: route.key,
113+
id: route.id,
114114
pattern,
115115
names,
116116
types,
@@ -127,6 +127,7 @@ export async function create_plugin(config, cwd) {
127127

128128
return {
129129
type: 'endpoint',
130+
id: route.id,
130131
pattern,
131132
names,
132133
types,

packages/kit/src/core/generate_manifest/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { s } from '../../utils/misc.js';
2-
import { parse_route_key } from '../../utils/routing.js';
2+
import { parse_route_id } from '../../utils/routing.js';
33
import { get_mime_lookup } from '../utils.js';
44

55
/**
@@ -72,7 +72,7 @@ export function generate_manifest({ build_data, relative_path, routes, format =
7272
],
7373
routes: [
7474
${routes.map(route => {
75-
const { pattern, names, types } = parse_route_key(route.key);
75+
const { pattern, names, types } = parse_route_id(route.id);
7676
7777
types.forEach(type => {
7878
if (type) validators.add(type);
@@ -81,7 +81,7 @@ export function generate_manifest({ build_data, relative_path, routes, format =
8181
if (route.type === 'page') {
8282
return `{
8383
type: 'page',
84-
key: ${s(route.key)},
84+
id: ${s(route.id)},
8585
pattern: ${pattern},
8686
names: ${s(names)},
8787
types: ${s(types)},
@@ -99,6 +99,7 @@ export function generate_manifest({ build_data, relative_path, routes, format =
9999
100100
return `{
101101
type: 'endpoint',
102+
id: ${s(route.id)},
102103
pattern: ${pattern},
103104
names: ${s(names)},
104105
types: ${s(types)},

packages/kit/src/core/sync/create_manifest_data/index.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ export default function create_manifest_data({
6161

6262
/**
6363
* @param {string} dir
64-
* @param {string[]} parent_key
64+
* @param {string[]} parent_id
6565
* @param {Part[][]} parent_segments
6666
* @param {string[]} parent_params
6767
* @param {Array<string|undefined>} layout_stack // accumulated __layout.svelte components
6868
* @param {Array<string|undefined>} error_stack // accumulated __error.svelte components
6969
*/
70-
function walk(dir, parent_key, parent_segments, parent_params, layout_stack, error_stack) {
70+
function walk(dir, parent_id, parent_segments, parent_params, layout_stack, error_stack) {
7171
/** @type {Item[]} */
7272
let items = [];
7373
fs.readdirSync(dir).forEach((basename) => {
@@ -135,7 +135,7 @@ export default function create_manifest_data({
135135
items = items.sort(comparator);
136136

137137
items.forEach((item) => {
138-
const key = parent_key.slice();
138+
const id = parent_id.slice();
139139
const segments = parent_segments.slice();
140140

141141
if (item.is_index) {
@@ -161,13 +161,13 @@ export default function create_manifest_data({
161161
}
162162

163163
segments[segments.length - 1] = last_segment;
164-
key[key.length - 1] += item.route_suffix;
164+
id[id.length - 1] += item.route_suffix;
165165
} else {
166166
segments.push(item.parts);
167167
}
168168
}
169169
} else {
170-
key.push(item.name);
170+
id.push(item.name);
171171
segments.push(item.parts);
172172
}
173173

@@ -201,7 +201,7 @@ export default function create_manifest_data({
201201

202202
walk(
203203
path.join(dir, item.basename),
204-
key,
204+
id,
205205
segments,
206206
params,
207207
layout_reset ? [layout_reset] : layout_stack.concat(layout),
@@ -236,7 +236,7 @@ export default function create_manifest_data({
236236

237237
routes.push({
238238
type: 'page',
239-
key: key.join('/'),
239+
id: id.join('/'),
240240
segments: simple_segments,
241241
pattern,
242242
params,
@@ -250,7 +250,7 @@ export default function create_manifest_data({
250250

251251
routes.push({
252252
type: 'endpoint',
253-
key: key.join('/'),
253+
id: id.join('/'),
254254
segments: simple_segments,
255255
pattern,
256256
file: item.file,
@@ -272,15 +272,15 @@ export default function create_manifest_data({
272272
const lookup = new Map();
273273
for (const route of routes) {
274274
if (route.type === 'page') {
275-
lookup.set(route.key, route);
275+
lookup.set(route.id, route);
276276
}
277277
}
278278

279279
let i = routes.length;
280280
while (i--) {
281281
const route = routes[i];
282-
if (route.type === 'endpoint' && lookup.has(route.key)) {
283-
lookup.get(route.key).shadow = route.file;
282+
if (route.type === 'endpoint' && lookup.has(route.id)) {
283+
lookup.get(route.id).shadow = route.file;
284284
routes.splice(i, 1);
285285
}
286286
}

packages/kit/src/core/sync/create_manifest_data/index.spec.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ test('creates routes', () => {
4242
assert.equal(routes, [
4343
{
4444
type: 'page',
45-
key: '',
45+
id: '',
4646
segments: [],
4747
pattern: /^\/$/,
4848
params: [],
@@ -54,7 +54,7 @@ test('creates routes', () => {
5454

5555
{
5656
type: 'page',
57-
key: 'about',
57+
id: 'about',
5858
segments: [{ rest: false, dynamic: false, content: 'about' }],
5959
pattern: /^\/about\/?$/,
6060
params: [],
@@ -66,7 +66,7 @@ test('creates routes', () => {
6666

6767
{
6868
type: 'endpoint',
69-
key: 'blog.json',
69+
id: 'blog.json',
7070
segments: [{ rest: false, dynamic: false, content: 'blog.json' }],
7171
pattern: /^\/blog\.json$/,
7272
file: 'samples/basic/blog/index.json.js',
@@ -75,7 +75,7 @@ test('creates routes', () => {
7575

7676
{
7777
type: 'page',
78-
key: 'blog',
78+
id: 'blog',
7979
segments: [{ rest: false, dynamic: false, content: 'blog' }],
8080
pattern: /^\/blog\/?$/,
8181
params: [],
@@ -87,7 +87,7 @@ test('creates routes', () => {
8787

8888
{
8989
type: 'endpoint',
90-
key: 'blog/[slug].json',
90+
id: 'blog/[slug].json',
9191
segments: [
9292
{ rest: false, dynamic: false, content: 'blog' },
9393
{ rest: false, dynamic: true, content: '[slug].json' }
@@ -99,7 +99,7 @@ test('creates routes', () => {
9999

100100
{
101101
type: 'page',
102-
key: 'blog/[slug]',
102+
id: 'blog/[slug]',
103103
segments: [
104104
{ rest: false, dynamic: false, content: 'blog' },
105105
{ rest: false, dynamic: true, content: '[slug]' }
@@ -128,7 +128,7 @@ test('creates routes with layout', () => {
128128
assert.equal(routes, [
129129
{
130130
type: 'page',
131-
key: '',
131+
id: '',
132132
segments: [],
133133
pattern: /^\/$/,
134134
params: [],
@@ -140,7 +140,7 @@ test('creates routes with layout', () => {
140140

141141
{
142142
type: 'page',
143-
key: 'foo',
143+
id: 'foo',
144144
segments: [{ rest: false, dynamic: false, content: 'foo' }],
145145
pattern: /^\/foo\/?$/,
146146
params: [],
@@ -224,7 +224,7 @@ test('disallows rest parameters inside segments', () => {
224224
assert.equal(routes, [
225225
{
226226
type: 'page',
227-
key: 'prefix-[...rest]',
227+
id: 'prefix-[...rest]',
228228
segments: [
229229
{
230230
dynamic: true,
@@ -241,7 +241,7 @@ test('disallows rest parameters inside segments', () => {
241241
},
242242
{
243243
type: 'endpoint',
244-
key: '[...rest].json',
244+
id: '[...rest].json',
245245
segments: [
246246
{
247247
dynamic: true,
@@ -308,7 +308,7 @@ test('allows multiple slugs', () => {
308308
[
309309
{
310310
type: 'endpoint',
311-
key: '[file].[ext]',
311+
id: '[file].[ext]',
312312
segments: [{ dynamic: true, rest: false, content: '[file].[ext]' }],
313313
pattern: /^\/([^/]+?)\.([^/]+?)$/,
314314
file: 'samples/multiple-slugs/[file].[ext].js',
@@ -330,7 +330,7 @@ test('ignores things that look like lockfiles', () => {
330330
assert.equal(routes, [
331331
{
332332
type: 'endpoint',
333-
key: 'foo',
333+
id: 'foo',
334334
segments: [{ rest: false, dynamic: false, content: 'foo' }],
335335
file: 'samples/lockfiles/foo.js',
336336
params: [],
@@ -354,7 +354,7 @@ test('works with custom extensions', () => {
354354
assert.equal(routes, [
355355
{
356356
type: 'page',
357-
key: '',
357+
id: '',
358358
segments: [],
359359
pattern: /^\/$/,
360360
params: [],
@@ -366,7 +366,7 @@ test('works with custom extensions', () => {
366366

367367
{
368368
type: 'page',
369-
key: 'about',
369+
id: 'about',
370370
segments: [{ rest: false, dynamic: false, content: 'about' }],
371371
pattern: /^\/about\/?$/,
372372
params: [],
@@ -378,7 +378,7 @@ test('works with custom extensions', () => {
378378

379379
{
380380
type: 'endpoint',
381-
key: 'blog.json',
381+
id: 'blog.json',
382382
segments: [{ rest: false, dynamic: false, content: 'blog.json' }],
383383
pattern: /^\/blog\.json$/,
384384
file: 'samples/custom-extension/blog/index.json.js',
@@ -387,7 +387,7 @@ test('works with custom extensions', () => {
387387

388388
{
389389
type: 'page',
390-
key: 'blog',
390+
id: 'blog',
391391
segments: [{ rest: false, dynamic: false, content: 'blog' }],
392392
pattern: /^\/blog\/?$/,
393393
params: [],
@@ -399,7 +399,7 @@ test('works with custom extensions', () => {
399399

400400
{
401401
type: 'endpoint',
402-
key: 'blog/[slug].json',
402+
id: 'blog/[slug].json',
403403
segments: [
404404
{ rest: false, dynamic: false, content: 'blog' },
405405
{ rest: false, dynamic: true, content: '[slug].json' }
@@ -411,7 +411,7 @@ test('works with custom extensions', () => {
411411

412412
{
413413
type: 'page',
414-
key: 'blog/[slug]',
414+
id: 'blog/[slug]',
415415
segments: [
416416
{ rest: false, dynamic: false, content: 'blog' },
417417
{ rest: false, dynamic: true, content: '[slug]' }
@@ -449,7 +449,7 @@ test('includes nested error components', () => {
449449
assert.equal(routes, [
450450
{
451451
type: 'page',
452-
key: 'foo/bar/baz',
452+
id: 'foo/bar/baz',
453453
segments: [
454454
{ rest: false, dynamic: false, content: 'foo' },
455455
{ rest: false, dynamic: false, content: 'bar' },
@@ -482,7 +482,7 @@ test('resets layout', () => {
482482
assert.equal(routes, [
483483
{
484484
type: 'page',
485-
key: '',
485+
id: '',
486486
segments: [],
487487
pattern: /^\/$/,
488488
params: [],
@@ -493,7 +493,7 @@ test('resets layout', () => {
493493
},
494494
{
495495
type: 'page',
496-
key: 'foo',
496+
id: 'foo',
497497
segments: [{ rest: false, dynamic: false, content: 'foo' }],
498498
pattern: /^\/foo\/?$/,
499499
params: [],
@@ -508,7 +508,7 @@ test('resets layout', () => {
508508
},
509509
{
510510
type: 'page',
511-
key: 'foo/bar',
511+
id: 'foo/bar',
512512
segments: [
513513
{ rest: false, dynamic: false, content: 'foo' },
514514
{ rest: false, dynamic: false, content: 'bar' }

packages/kit/src/core/sync/write_manifest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function write_manifest(manifest_data, base, output) {
3535
const tuple = [get_indices(route.a), get_indices(route.b)];
3636
if (route.shadow) tuple.push('1');
3737
38-
return `${s(route.key)}: [${tuple.join(', ')}]`;
38+
return `${s(route.id)}: [${tuple.join(', ')}]`;
3939
}
4040
})
4141
.filter(Boolean)

0 commit comments

Comments
 (0)