-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: Vite environment API #14008
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
feat: Vite environment API #14008
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
f77bc70
wip
71e3d5a
Merge branch 'main' into environment-api
498ef93
add files to files array
b3d902f
bump peer dep
eaaa487
Merge branch 'main' into environment-api
d5b7786
use node 20 types
ef4d10e
ignore virtual modules when optimising deps
a02fbfa
try this to avoid prebundling kit in worker
5d0ede5
Merge branch 'main' into environment-api
4ca01c9
export server from different path
21ee281
optimise cjs deps
9158a25
with dep optimisation
f4eb279
without dep optimisation
99d8a2f
patch until cloudflare vite plugin releases
7f1feb1
correctly handle static assets in dev
911717f
Merge branch 'main' into environment-api
2ee2c2c
always optimize deps
463cd5d
remove workaround
7fdfe68
fix svelte context not being set correctly
ad109ad
im changing latops so just commit wip
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import { Server } from '@sveltejs/kit/internal/server'; | ||
| // TODO: use prod variables when building | ||
| // TODO: fix kit virtual module type issues when this file is consumed by an app | ||
| import { version } from '__sveltekit/environment'; | ||
| import { | ||
| manifest, | ||
| env, | ||
| remote_address, | ||
| base_path, | ||
| prerendered | ||
| } from '__sveltekit/vite-environment'; | ||
| import * as Cache from 'worktop/cfw.cache'; | ||
|
|
||
| const app_path = `/${manifest.appPath}`; | ||
|
|
||
| const immutable = `${app_path}/immutable/`; | ||
| const version_file = `${app_path}/version.json`; | ||
|
|
||
| const server = new Server(manifest); | ||
|
|
||
| await server.init({ env }); | ||
|
|
||
| /** | ||
| * @param {Request} request | ||
| * @param {{ ASSETS: { fetch: typeof fetch } }} env | ||
| * @param {import('@cloudflare/workers-types').ExecutionContext} ctx | ||
| * @returns {Promise<Response>} | ||
| */ | ||
| export async function handleRequest(request, env, ctx) { | ||
| // skip cache if "cache-control: no-cache" in request | ||
| let pragma = request.headers.get('cache-control') || ''; | ||
| let res = !pragma.includes('no-cache') && (await Cache.lookup(request)); | ||
| if (res) return res; | ||
|
|
||
| let { pathname, search } = new URL(request.url); | ||
| try { | ||
| pathname = decodeURIComponent(pathname); | ||
| } catch { | ||
| // ignore invalid URI | ||
| } | ||
|
|
||
| const stripped_pathname = pathname.replace(/\/$/, ''); | ||
|
|
||
| // files in /static, the service worker, and Vite imported server assets | ||
| let is_static_asset = false; | ||
| const filename = stripped_pathname.slice(base_path.length + 1); | ||
| if (filename) { | ||
| is_static_asset = | ||
| manifest.assets.has(filename) || | ||
| manifest.assets.has(filename + '/index.html') || | ||
| filename in manifest._.server_assets || | ||
| filename + '/index.html' in manifest._.server_assets; | ||
| } | ||
|
|
||
| let location = pathname.at(-1) === '/' ? stripped_pathname : pathname + '/'; | ||
|
|
||
| // TODO: we should only return the version var in dev because the version file is not written to disk | ||
| if (pathname === version_file) { | ||
| res = new Response(version); | ||
| // TODO: only in production | ||
| // } | ||
| // else if ( | ||
| // is_static_asset || | ||
| // prerendered.has(pathname) || | ||
| // pathname === version_file || | ||
| // pathname.startsWith(immutable) | ||
| // ) { | ||
| // res = await env.ASSETS.fetch(request); | ||
| } else if (location && prerendered.has(location)) { | ||
| // trailing slash redirect for prerendered pages | ||
| if (search) location += search; | ||
| res = new Response('', { | ||
| status: 308, | ||
| headers: { | ||
| location | ||
| } | ||
| }); | ||
| } else { | ||
| // dynamically-generated pages | ||
| res = await server.respond(request, { | ||
| platform: { | ||
| env, | ||
| ctx, | ||
| context: ctx, // deprecated in favor of ctx | ||
| // @ts-expect-error webworker types from worktop are not compatible with Cloudflare Workers types | ||
| caches, | ||
| // @ts-expect-error the type is correct but ts is confused because platform.cf uses the type from index.ts while req.cf uses the type from index.d.ts | ||
| cf: request.cf | ||
| }, | ||
| getClientAddress() { | ||
| if (remote_address) return remote_address; | ||
| throw new Error('Could not determine clientAddress'); | ||
| // TODO: use the header in prod | ||
| // return request.headers.get('cf-connecting-ip'); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| // write to `Cache` only if response is not an error, | ||
| // let `Cache.save` handle the Cache-Control and Vary headers | ||
| pragma = res.headers.get('cache-control') || ''; | ||
| return pragma && res.status < 400 ? Cache.save(request, res, ctx) : res; | ||
| } | ||
|
|
||
| // Without this, server file changes will invalidate the entire server module graph | ||
| if (import.meta.hot) { | ||
| import.meta.hot.accept(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just curious, is there a reason you had to do this? is there anything you need me to fix in it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Related to benmccann/esm-env#20