Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError when using a value from PageLoad when navigating #13276

Open
AndersRobstad opened this issue Jan 6, 2025 · 2 comments
Open

TypeError when using a value from PageLoad when navigating #13276

AndersRobstad opened this issue Jan 6, 2025 · 2 comments

Comments

@AndersRobstad
Copy link

AndersRobstad commented Jan 6, 2025

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

@MathiasWP
Copy link
Contributor

MathiasWP commented Jan 6, 2025

This bug is bad, it makes it tedious for us to save changed inputs when the user navigates with the back/forward buttons in their browser after having typed something, and we have no understanding of how we are triggering this.

@eltigerchino
Copy link
Member

eltigerchino commented Jan 7, 2025

This happens because the page props (such as page.data) are updated when we trigger a re-render. However, the blur event only occurs after the re-render.

root.$set(navigation_result.props);
update(navigation_result.props.page);
has_navigated = true;
} else {
initialize(navigation_result, target, false);
}
const { activeElement } = document;
// need to render the DOM before we can scroll to the rendered elements and do focus management
await tick();

I'm not sure what the fix is here. Re-rendering before updating the page props to cause the blur event to trigger first? Not sure what unintended side effects this could have.

	const { activeElement } = document; 

	if (started) {
        // unmount current components to trigger blur event with old data
		root.$set({ constructors: [] });
		await tick();

        // render new page with new data
		root.$set(navigation_result.props);
		update(navigation_result.props.page);
		has_navigated = true;
	}

	// need to render the DOM before we can scroll to the rendered elements and do focus management
	await tick();

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

No branches or pull requests

3 participants