question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

+layout.server.ts load re-runs on every navigation

See original GitHub issue

Describe the bug

Layout load function runs on every navigation

Reproduction

Log to console when layout load function is executed and it happens on every navigation.

import type { LayoutServerLoad } from './$types'

export const load: LayoutServerLoad = async ({ }) => {
  console.log('load /layout')

  return {
    name: 'My Site'
  }
}

Logs

No response

System Info

npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.64 
    @sveltejs/kit: next => 1.0.0-next.415 
    svelte: ^3.44.0 => 3.49.0 
    vite: ^3.0.0 => 3.0.8

Severity

blocking an upgrade

Additional Information

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:6
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Rich-Harriscommented, Aug 17, 2022

An argument against posting data back to the server is that we may want to introduce the ability to send non-JSONable values from the server:

// +layout.server.js
export function load() {
  const map = new Map();
  map.set(map, map);

  return {
    map
  };
}

It’s likely that we’d achieve that with https://github.com/rich-harris/devalue, which is only safe to use when sending data from the server to the client, not the other way around.

0reactions
Rich-Harriscommented, Aug 17, 2022

This Remix issue asking for loaders to use superjson has 37 👍 votes — it’s definitely not an unheard of requirement to include things like Date objects etc.

Another case that JSON can’t handle is this, if the referential equality matters (which it would in a <select bind:value> for example):

export function load() {
  const options = [
    { flavour: 'chocolate' },
    { flavour: 'strawberry' },
    { flavour: 'banana' }
  ];

  const selected = options[0];

  return { options, selected };
}

We don’t plan on using superjson, because devalue is faster, produces smaller output, and doesn’t force you to add a not-that-tiny client to deserialize on the client. But it does mean the data can only safely go one way. (Though we decided that sending data back to the server to simulate statefulness is a terrible idea anyway.)

(Re cache aware server push — the logic is on the wrong side. The server doesn’t need to know what the client has, because the client is going to tell it. All the server has to do is tell the client ‘this blob of data will be invalidated if params.foo changes’ or whatever.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Loading data • Docs • SvelteKit
A load function in a +page.js file runs both on the server and in the browser. ... +layout.svelte component above it, has access...
Read more >
Sveltekit question: How to force loads to rerun for a page.server
Anyway i can force the layout to rerrun the load function? +page.server.ts import { redirect, type Cookies } from '@sveltejs/kit'; ...
Read more >
Understanding Next.js routeChangeStart and router events
Next.js routeChangeStart and other router events make it possible for us to listen to events regarding navigations from one page to another.
Read more >
Angular Breadcrumbs with Complex Routing and Navigation
Refreshing the current browser screen exactly repeats the existing activated route and URL but it does cause the problem of losing the component ......
Read more >
AngularJS to Angular concepts: Quick reference
The *ngFor directive in Angular is like the ng-repeat directive in AngularJS. It repeats the associated DOM element for each item in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found