Skip to content

Conversation

eltigerchino
Copy link
Member

@eltigerchino eltigerchino commented Aug 13, 2025

fixes #14143

This PR adds a condition before referencing the value of __SVELTEKIT_PAYLOAD__ so that it doesn't error when it doesn't exist during unit testing.


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

Copy link

changeset-bot bot commented Aug 13, 2025

🦋 Changeset detected

Latest commit: f1c2d9b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@svelte-docs-bot
Copy link

@@ -179,7 +179,9 @@ let target;
export let app;

/** @type {Record<string, any>} */
export const remote_responses = __SVELTEKIT_PAYLOAD__.data ?? {};
// avoid referencing `__SVELTEKIT_PAYLOAD__` because it will be undefined unless this module was loaded by Vite
Copy link
Member

Choose a reason for hiding this comment

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

I don't fully understand yet why Vitest wouldn't load this through Vite module loader yet. If anything, this comment needs to be enhanced a bit to clarify this.
I also don't fully understand why this global is causing problems but the other globals in this file do not - would they also cause problems if we started using them for more than booleans, i.e. property access?

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right. I was completely misunderstanding the issue. Vite does perform the global variable replacements. It's just that our globalThis.__sveltekit_dev object is undefined unless it's defined by our server rendered page.

So the only issue was accessing a property on an undefined object. I'll just add an optional chain.

Copy link
Member

Choose a reason for hiding this comment

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

Inside vite/index.js we have

// @ts-ignore this prevents a reference error if `client.js` is imported on the server
globalThis.__sveltekit_dev = {};

I wonder why that isn't working? Is it because for Vitest the if (build) branch is used? If so, should we instead add a "is this Vitest starting this build if so add the globalThis in here like we do in dev"? Or could this cause problems in other places?

Copy link
Member Author

@eltigerchino eltigerchino Aug 15, 2025

Choose a reason for hiding this comment

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

Inside vite/index.js we have

// @ts-ignore this prevents a reference error if `client.js` is imported on the server
globalThis.__sveltekit_dev = {};

I wonder why that isn't working? Is it because for Vitest the if (build) branch is used?

Definitely not because of the if (build) branch. I can reproduce the issue here where the globalThis is not the same when logging it in the test file after running pnpm test: https://stackblitz.com/edit/vitejs-vite-tycr5kae
Maybe it's because test files are run in a different thread so they don't share the same globalThis? https://vitest.dev/guide/features.html#threads

@Rich-Harris Rich-Harris merged commit 996ff5f into main Aug 21, 2025
21 checks passed
@Rich-Harris Rich-Harris deleted the fix-payload-undefined branch August 21, 2025 01:20
@github-actions github-actions bot mentioned this pull request Aug 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Import from $app/navigation results in undefined property error
3 participants