kit icon indicating copy to clipboard operation
kit copied to clipboard

The `+page.server.js`, `+layout.server.js`, `hooks.server.js` will not load after click browser's back button from non-client-side-routing page

Open Splendith opened this issue 4 years ago • 2 comments

Describe the bug

The +page.server.js, +layout.server.js, hooks.server.js will not loaded after click browser's back button from non-client-side-routing page (e.g., external link).

I don't know whether it's the bug or not. But it's hard to control the user flow of that page in this situation. Sorry if I misunderstood something.

Reproduction

Repository

https://github.com/Splendith/sveltekit-test

Installation

Clone the repo and then,

npm install
npm run dev -- --open

Description

This little example is the home page that show the random number received from +page.server.js, for demonstrating the bug reproduction.

How to reproduce

  • Open this project via web browser.
  • Click "Go to another internal page" and then go back to homepage. Whenever you click "Go home" link or click browser's "back" button. The +page.server.js in /routes/ will load and regenerate a random number normally.
  • Click "Go to external Google.com" and then click browser's "back" button. The +page.server.js will no longer load. The log from the +page.server.js not show, the new random number is not generated, and it looks like they use the random value from the first page load.
  • When in home page (/). Type http://127.0.0.1:5173/another manually in address bar and then click back button. The +page.server.js will also not load.

It also happens in +layout.server.js, hooks.server.js too, but I've omitted it for simple demonstration.

I've tried it in Microsoft Edge and Firefox.

Logs

No response

System Info

System:
	OS: Windows 10 10.0.22621
	CPU: (16) x64 Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz
	Memory: 32.63 GB / 63.93 GB
Binaries:
	Node: 16.15.1 - C:\Program Files\nodejs\node.EXE
	Yarn: 1.22.5 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
	npm: 8.12.2 - C:\Program Files\nodejs\npm.CMD
Browsers:
	Edge: 44.22621.521.0
	Internet Explorer: 11.0.22621.1
npmPackages:
	svelte: ^3.50.1 => 3.50.1
	vite: ^3.1.3 => 3.1.3

Severity

serious, but I can work around it

Additional Information

No response

Splendith avatar Sep 20 '22 13:09 Splendith

I believe this is the expected browser behavior. Browser aggressively cache the state of the previous page, so it's like you're resuming when coming back, not like starting again from scratch. Not sure what's the best way to solve this, but possibly some window event that hints that the page is resumed, not loaded from scratch.

dummdidumm avatar Sep 20 '22 13:09 dummdidumm

Yep, the browser is storing the initial response in the disk cache, and then restoring from there when you navigate back. This is quite surprising behaviour but it's also just how browsers work, and it probably isn't our place to interfere with that at a framework level.

The way to prevent it is by setting a cache-control: no-store header on the response, either in your +page.server.js with setHeaders or in your handle hook with response.headers.set(...).

Rich-Harris avatar Sep 20 '22 13:09 Rich-Harris