sveltejs / sveltejs/kit

Add an option to distinguish between current navigation load event and `preloadData` load event in server hooks

Open
#12,559 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the problem

I'm implementing server-side analytics, and I'm using load functions to report user navigation. Some page:

export const load = (event) => {
  event.locals.trackNavigation(event);
};

The problem is some pages do preloadData which then results in multiple navigation events, even though user navigated from one page to another. Client-side tracking is not an option in my case. I wanted to differentiate events in the trackNavigation function, but I couldn't find any determining factor to distinguish navigation from preloading.

I've looked up differences in server hooks events, load functions events, client hooks. I also tried to intercept fetch requests in a service worker to maybe differentiate fetch requests there, but I couldn't find a way to spot a difference.

The very only situation in which I could tell which one is which is the initial session request, which does not include __data.json in the pathname. Later, each request contains the __data.json with an invalidation parameter.

Describe the proposed solution
  • Extend the preloadData method with options parameter that later can be consumed on server side in hooks or load functions.
  • Hardcode information that the given request was invoked by the preloadData function or is a navigation event.
Alternatives considered

My goal is to implement server-side tracking. If there's any other option to do it while having preloadData functions in pages and layouts I'd be happy to change the approach.

Importance

would make my life easier

Additional Information

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the preloadData API alongside the server hooks and load function event documentation, focusing on how __data.json requests are handled. Define and verify a reliable distinction between preload and navigation requests, including how the proposed options would be consumed server-side.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
backend, web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.