Skip to content

Layout data is cleared when url is used in +layout.ts #6336

@alialaa

Description

@alialaa

Describe the bug

I was having some simple redirect logic in my application in +layout.ts:

export const load = async ({ data, url }) => {
	const { user } = data || {};
	if (user && url.pathname === '/login') {
		throw redirect(302, '/');
	}
	if (!user && url.pathname !== '/login') {
		throw redirect(302, '/login');
	}
	return {
		user
	};
};

After updating to @sveltejs/[email protected], the app was fully reloading when navigating instead of client side routing. After some investigation I found out that using url in +layout causes the data passed from +layout.server to be cleared.

In the example below I am just logging url.pathname in +layout and displaying the data in the Home page. When navigating from and the back to the home page; the data is cleared. That won't happen if console.log(url.pathname) is removed from +layout.

Reproduction

https://stackblitz.com/edit/sveltejs-kit-template-default-bdr4bj?file=src/routes/+layout.js

Logs

No response

System Info

System:
    OS: macOS 12.1
    CPU: (8) arm64 Apple M1
    Memory: 1.63 GB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.16.0 - /usr/local/bin/node
    Yarn: 1.22.11 - ~/.npm-global/bin/yarn
    npm: 8.11.0 - /usr/local/bin/npm
    Watchman: 2022.03.21.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 104.0.5112.101
    Safari: 15.2
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.66 
    @sveltejs/kit: ^1.0.0-next.423 => 1.0.0-next.444 
    svelte: ^3.44.0 => 3.49.0 
    vite: ^3.0.0 => 3.0.9

Severity

blocking an upgrade

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions