sveltejs / sveltejs/kit

Different url.origin with browser load() vs server load() using vite

Open
#6,676 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Describe the bug

I am running Sveltekit behind a reverse proxy which does the TLS/HTTPS termination. It then passes all requests using HTTP to Sveltekit.

  • My production build uses the nodejs adapter, which allows setting the origin through an env var, e.g. ORIGIN=https://my.domain. In this case, browser load({url}) and server load({url}) are identical and using the value of ORIGIN.

  • Is there a way to configure something similar in dev? Currently, url.origin returns different values for browser load() and server load() when using either 'vite dev' or 'vite preview'

Browser load({url}) returns url.origin = https://...
Server load({url}) returns url.orgin = http://...

Reproduction
# same issue with both commands:
vite dev --host 0.0.0.0
vite preview --host 0.0.0.0

// +page.ts

/** @type {import('./$types').PageLoad} */
export async function load({ url }: PageLoadEvent) {
    console.log(url.origin) // http://... is returned during SSR, https://... is returned during CSR
}
Logs

No response

System Info
System:
    OS: Linux 4.18 Red Hat Enterprise Linux 8.6 (Ootpa)
    CPU: (2) x64 Intel(R) Xeon(R) Gold 6240R CPU @ 2.40GHz
    Memory: 8.24 GB / 15.46 GB
    Container: Yes
    Shell: 4.4.20 - /usr/bin/bash
  Binaries:
    Node: 18.8.0 - /usr/bin/node
    npm: 8.18.0 - /usr/bin/npm
  npmPackages:
    @sveltejs/adapter-node: next => 1.0.0-next.88 
    @sveltejs/kit: next => 1.0.0-next.477 
    svelte: ^3.44.0 => 3.50.0 
    vite: ^3.1.0 => 3.1.0

Browser = Brave, Version 1.43.89 Chromium: 105.0.5195.102 (Official Build) (64-bit)
Severity

serious, but I can work around it

Additional Information

Besides causing CORS issues, mismatching url.origin for browser vs server is also causing an inconsistent behavior with fetch. This is because fetch appears to skip making an HTTP call on the server if the origin matches the fetched URL, but it doesn't skip the HTTP call if the url.origin is using a different protocol (e.g. http) than the fetched URL (e.g. https).

Being able to force the origin to a value in dev, similar to how we can set the ORIGIN variable with the nodejs adapter would be an acceptable fix to me.

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

Reproduce the mismatch with vite dev --host 0.0.0.0 and vite preview --host 0.0.0.0, using the load function in +page.ts to compare browser and server url.origin. Trace how the dev and preview entry points determine the origin, then verify that both paths use the configured HTTPS origin and that the related fetch behavior is consistent.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript, vite
Domain
backend, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.