sveltejs / sveltejs/kit

preloadCode does not handle relative URLs

Open
#10,892 0 comments 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 bug

I noticed that the preloadCode function behaves differently than preloadData: preloadCode requires an absolute URL while preloadData works well both with relative and absolute URLs.

The documentation does not mention the requirement specific to preloadCode and lets us think that preloadCode and preloadData are very similar.

Could preloadCode be amended to accept relative URLs in addition to absolute ones?
Could the documentation could be amended to state the kind of URL the methods accept?

If for some reason, preloadCode cannot accept relative URLs, maybe it should throw or at least log a warning? Currently, we have no way to know nothing is done with a relative URL.

Reproduction

Here is repro with links and buttons to preload a page in a subfolder named "x".

  1. When we hover over the preloadCode link (with data-sveltekit-preload-code="hover"), there is a request for x/+page.svelte
  2. When we hover over the preloadData link (with data-sveltekit-preload-data="hover"), there is a request for x/+page.svelte
  3. When we click the preloadCode("x") button, there is no request for x/+page.svelte
  4. When we click the preloadData("x") button, there is a request for x/+page.svelte
  5. When we click the preloadCode("/x") button, there is a request for x/+page.svelte
  6. When we click the preloadCode("/x") button, there is a request for x/+page.svelte

⚠️ Refresh the preview before each step above and look at the network tab of the dev tools of your browser

Note that the href for both links is "x", so preloading works well with a relative URL on a link.

Logs

No response

System Info
Tested with Firefox and Chrome

  Binaries:
    Node: 18.17.1 - C:\Program Files\nodejs\node.EXE
    npm: 9.6.7 - C:\Program Files\nodejs\npm.CMD
    pnpm: 8.9.0 - ~\AppData\Local\pnpm\pnpm.EXE
  Browsers:
    Edge: Chromium (118.0.2088.46)
    Internet Explorer: 11.0.22621.1
  npmPackages:
    svelte: ^4.2.0 => 4.2.0
Severity

annoyance

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 reproducing the relative-versus-absolute URL behavior described for preloadCode and preloadData in the linked SvelteLab example, while watching the browser network tab. Trace the preloadCode and preloadData entry points and determine whether relative URLs should trigger the x/+page.svelte request or produce an explicit warning; update the documented URL behavior accordingly.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend, web-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.