TypeError when using a value from PageLoad when navigating
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 20.8k
- Forks
- 2.3k
- Avg merge
- 1d 16h
- Merged PRs (30d)
- 156
Description
Describe the bug
When having a dependency to some data that originates from the PageLoad function that is used when the user navigates a TypeError is thrown due to the data being undefined at the moment of access. It seems that the data from the load-function is set to undefined before the navigation completes, causing the components below to use the undefined value. This problem only happens when the value comes from the page load (+page.ts), and does not occur if the value is defined in the +page.svelte file.
Reproduction
Code for repro can be found here: https://github.com/AndersRobstad/svelte-navigation-bug
Given this folder structure:
- routes/
- textarea/
- +page.svelte
- +page.ts
- +page.svelte
- textarea/
Where the +page.svelte and +page.ts inside the textarea route looks like this:
//+page.ts
import type { PageLoad } from './$types';
export const load: PageLoad = function () {
return {
dataDependencyFromLoad: { id: 'ID' },
};
};
<script lang="ts">
import type { PageData } from './$types';
import Textarea from "./Textarea.svelte";
interface Props {
data: PageData
}
const { data }: Props = $props();
function handleChange(value: string) {
console.log("Blur event happened: ", {value, id: data.dataDependencyFromLoad.id});
}
</script>
<p>Write something below without blurring the input and navigate back to the main page.</p>
<Textarea value="initial value" onBlur={handleChange}/>
Reproduction of the bug:
- navigate to the textarea route
- change the contents of the textarea
- navigate back to the home page (without having blurred the textarea yet)
The following error will be thrown
Uncaught TypeError: Cannot read properties of undefined (reading 'id')
This stops occurring if the dataDependencyFromLoad is defined in the +page.svelte file instead.
Logs
No response
System Info
System:
OS: macOS 14.3.1
CPU: (10) arm64 Apple M2 Pro
Memory: 627.34 MB / 16.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 20.11.0 - ~/.nvm/versions/node/v20.11.0/bin/node
Yarn: 1.22.22 - ~/.nvm/versions/node/v20.11.0/bin/yarn
npm: 10.2.4 - ~/.nvm/versions/node/v20.11.0/bin/npm
pnpm: 9.14.3 - ~/Library/pnpm/pnpm
Browsers:
Chrome: 131.0.6778.205
Edge: 131.0.2903.112
Safari: 17.3.1
npmPackages:
@sveltejs/adapter-auto: ^3.0.0 => 3.3.1
@sveltejs/kit: ^2.0.0 => 2.15.1
@sveltejs/vite-plugin-svelte: ^4.0.0 => 4.0.4
svelte: ^5.0.0 => 5.16.1
vite: ^5.4.11 => 5.4.11
Severity
serious, but I can work around it
Additional Information
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked reproduction and its routes/textarea/+page.ts and +page.svelte files. Reproduce the navigation sequence with the listed SvelteKit versions, then trace how the PageLoad data is handled during navigation. Done means the blur handler no longer observes the load value as undefined after navigating away.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- frontend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100