Skip to content

TypeError when using a value from PageLoad when navigating #13276

@AndersRobstad

Description

@AndersRobstad

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

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:

  1. navigate to the textarea route
  2. change the contents of the textarea
  3. 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.

Skjermopptak 2025-01-06 kl  10 02 04

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions