Skip to content

Commit 4995112

Browse files
feat: add universal IDs to client nodes
1 parent 3459122 commit 4995112

File tree

7 files changed

+45
-46
lines changed

7 files changed

+45
-46
lines changed

packages/kit/src/core/config/index.spec.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,6 @@ test('errors on loading config with incorrect default export', async () => {
383383
});
384384

385385
test('accepts valid tracing values', () => {
386-
// Test boolean values
387386
assert.doesNotThrow(() => {
388387
validate_config({
389388
kit: {
@@ -400,7 +399,6 @@ test('accepts valid tracing values', () => {
400399
});
401400
});
402401

403-
// Test string values
404402
assert.doesNotThrow(() => {
405403
validate_config({
406404
kit: {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ export function write_client_manifest(kit, manifest_data, output, metadata) {
2929
);
3030
}
3131

32+
if (kit.tracing === 'client' || kit.tracing === true) {
33+
// we don't need to include these if tracing is disabled -- they make the manifest bigger
34+
declarations.push(`export const universal_id = ${s(node.universal)};`);
35+
}
36+
3237
if (node.component) {
3338
declarations.push(
3439
`export { default as component } from ${s(

packages/kit/src/runtime/client/client.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ import {
3636
import { validate_page_exports } from '../../utils/exports.js';
3737
import { compact } from '../../utils/array.js';
3838
import { HttpError, Redirect, SvelteKitError } from '../control.js';
39-
import { INVALIDATED_PARAM, TRAILING_SLASH_PARAM, validate_depends } from '../shared.js';
39+
import {
40+
INVALIDATED_PARAM,
41+
TRAILING_SLASH_PARAM,
42+
validate_depends,
43+
validate_load_response
44+
} from '../shared.js';
4045
import { get_message, get_status } from '../../utils/error.js';
4146
import { writable } from 'svelte/store';
4247
import { page, update, navigating } from './state.svelte.js';
@@ -759,7 +764,7 @@ async function load_node({ loader, parent, url, params, route, server_data_node
759764
const tracer = await get_tracer({ is_enabled: app.tracing });
760765

761766
return record_span({
762-
name: 'sveltekit.load.universal',
767+
name: 'sveltekit.load',
763768
tracer,
764769
attributes: {
765770
'sveltekit.load.node_id': node.universal_id || 'unknown',
@@ -776,19 +781,11 @@ async function load_node({ loader, parent, url, params, route, server_data_node
776781
lock_fetch();
777782
data = await traced_load();
778783

779-
if (data != null && Object.getPrototypeOf(data) !== Object.prototype) {
780-
throw new Error(
781-
`the load function located in ${node.universal_id} returned ${
782-
typeof data !== 'object'
783-
? `a ${typeof data}`
784-
: data instanceof Response
785-
? 'a Response object'
786-
: Array.isArray(data)
787-
? 'an array'
788-
: 'a non-plain object'
789-
}, but must return a plain object at the top level (i.e. \`return {...}\`)`
790-
);
791-
}
784+
validate_load_response(
785+
data,
786+
// universal_id isn't populated if tracing is disabled because it adds otherwise unnecessary bloat to the manifest
787+
node.universal_id ? `in ${node.universal_id}` : `related to route '${route.id}'`
788+
);
792789
} finally {
793790
unlock_fetch();
794791
}

packages/kit/src/runtime/server/page/actions.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,8 @@ async function call_action(event, actions, tracing) {
267267
'sveltekit.action.result.type': 'failure',
268268
'sveltekit.action.result.status': result.status
269269
});
270-
} else {
271-
action_span.setAttributes({
272-
'sveltekit.action.result.type': 'success'
273-
});
274270
}
271+
275272
return result;
276273
}
277274
});

packages/kit/src/runtime/server/page/load_data.js

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { DEV } from 'esm-env';
22
import { disable_search, make_trackable } from '../../../utils/url.js';
3-
import { validate_depends } from '../../shared.js';
3+
import { validate_depends, validate_load_response } from '../../shared.js';
44
import { b64_encode } from '../../utils.js';
55
import { with_event } from '../../app/server/event.js';
66
import { record_span } from '../../telemetry/record_span.js';
@@ -74,11 +74,12 @@ export async function load_server_data({ event, state, node, parent, tracing })
7474
const tracer = await get_tracer({ is_enabled: tracing });
7575

7676
const result = await record_span({
77-
name: 'sveltekit.load.server',
77+
name: 'sveltekit.load',
7878
tracer,
7979
attributes: {
8080
'sveltekit.load.node_id': node.server_id || 'unknown',
8181
'sveltekit.load.type': 'server',
82+
'sveltekit.load.environment': 'server',
8283
'sveltekit.route.id': event.route.id || 'unknown'
8384
},
8485
fn: async () => {
@@ -176,7 +177,7 @@ export async function load_server_data({ event, state, node, parent, tracing })
176177
});
177178

178179
if (__SVELTEKIT_DEV__) {
179-
validate_load_response(result, node.server_id);
180+
validate_load_response(result, `in ${node.server_id}`);
180181
}
181182

182183
done = true;
@@ -226,7 +227,7 @@ export async function load_data({
226227
const tracer = await get_tracer({ is_enabled: tracing });
227228

228229
const result = await record_span({
229-
name: 'sveltekit.load.universal',
230+
name: 'sveltekit.load',
230231
tracer,
231232
attributes: {
232233
'sveltekit.load.node_id': node.universal_id || 'unknown',
@@ -252,7 +253,7 @@ export async function load_data({
252253
});
253254

254255
if (__SVELTEKIT_DEV__) {
255-
validate_load_response(result, node.universal_id);
256+
validate_load_response(result, `in ${node.universal_id}`);
256257
}
257258

258259
return result ?? null;
@@ -436,23 +437,3 @@ async function stream_to_string(stream) {
436437
}
437438
return result;
438439
}
439-
440-
/**
441-
* @param {any} data
442-
* @param {string} [id]
443-
*/
444-
function validate_load_response(data, id) {
445-
if (data != null && Object.getPrototypeOf(data) !== Object.prototype) {
446-
throw new Error(
447-
`a load function in ${id} returned ${
448-
typeof data !== 'object'
449-
? `a ${typeof data}`
450-
: data instanceof Response
451-
? 'a Response object'
452-
: Array.isArray(data)
453-
? 'an array'
454-
: 'a non-plain object'
455-
}, but must return a plain object at the top level (i.e. \`return {...}\`)`
456-
);
457-
}
458-
}

packages/kit/src/runtime/shared.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,23 @@ export function validate_depends(route_id, dep) {
1414
export const INVALIDATED_PARAM = 'x-sveltekit-invalidated';
1515

1616
export const TRAILING_SLASH_PARAM = 'x-sveltekit-trailing-slash';
17+
18+
/**
19+
* @param {any} data
20+
* @param {string} [location_description]
21+
*/
22+
export function validate_load_response(data, location_description) {
23+
if (data != null && Object.getPrototypeOf(data) !== Object.prototype) {
24+
throw new Error(
25+
`a load function ${location_description} returned ${
26+
typeof data !== 'object'
27+
? `a ${typeof data}`
28+
: data instanceof Response
29+
? 'a Response object'
30+
: Array.isArray(data)
31+
? 'an array'
32+
: 'a non-plain object'
33+
}, but must return a plain object at the top level (i.e. \`return {...}\`)`
34+
);
35+
}
36+
}

packages/kit/src/types/internal.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export interface CSRPageNode {
111111
load?: Load;
112112
trailingSlash?: TrailingSlash;
113113
};
114+
universal_id?: string;
114115
}
115116

116117
export type CSRPageNodeLoader = () => Promise<CSRPageNode>;

0 commit comments

Comments
 (0)