Skip to content
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

[feat] adapter module #2285

Closed
wants to merge 26 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
cbfd2cf
create proxy for adapters to import from
ignatiusmb Aug 25, 2021
f94fbe8
adapter ambient module
ignatiusmb Aug 25, 2021
11b99fc
adapter node sync
ignatiusmb Aug 25, 2021
8fde219
update constants
ignatiusmb Aug 25, 2021
f9bff84
add to exports
ignatiusmb Aug 25, 2021
09d007c
promisify module export types
ignatiusmb Aug 25, 2021
a76784b
properly type default export
ignatiusmb Aug 25, 2021
17a9bbe
shorter namings
ignatiusmb Aug 25, 2021
409b239
revert some paths
ignatiusmb Aug 25, 2021
09cf6a9
Merge branch 'master' of https://github.com/sveltejs/kit into adapter…
ignatiusmb Aug 25, 2021
6f50f5b
tidy up imports
ignatiusmb Sep 2, 2021
b07e677
@sveltejs/esbuild-plugin-app-resolver
ignatiusmb Sep 2, 2021
f069539
allow importing app output from @sveltejs/kit/app
ignatiusmb Sep 2, 2021
61741f1
Merge branch 'master' of https://github.com/sveltejs/kit into adapter…
ignatiusmb Sep 2, 2021
4abc0a2
resolve merges
ignatiusmb Sep 2, 2021
6cb5be2
use appResolver for adapter-node
ignatiusmb Sep 2, 2021
8fd0c4c
update adapter api docs
ignatiusmb Sep 2, 2021
e0602fc
lockfile
ignatiusmb Sep 2, 2021
e84a688
adapter node tests
ignatiusmb Sep 2, 2021
fa20353
revert constants changes
ignatiusmb Sep 2, 2021
88d541a
remove promise wrapper from types
ignatiusmb Sep 2, 2021
e1af57c
update rest of adapter import paths
ignatiusmb Sep 2, 2021
d44975b
remove accidental type as callback
ignatiusmb Sep 2, 2021
9dff930
some wording
ignatiusmb Sep 3, 2021
9ab01b5
Merge branch 'master' into adapter-module
ignatiusmb Sep 13, 2021
15f6149
fix merge conflict
ignatiusmb Sep 13, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge branch 'master' of https://github.com/sveltejs/kit into adapter…
…-module
ignatiusmb committed Sep 2, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 61741f157b856a782139a52ea8aa6be5938a4e84
5 changes: 5 additions & 0 deletions .changeset/blue-bulldogs-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[fix] upgrade to Vite 2.5.2 to fix URL decoding
5 changes: 5 additions & 0 deletions .changeset/few-suits-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[fix] don't de-indent user-rendered HTML
5 changes: 5 additions & 0 deletions .changeset/fresh-adults-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Allow service workers to access files using the $lib alias
5 changes: 5 additions & 0 deletions .changeset/nine-beans-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-node': patch
---

[feat] expose handler to allow use in custom server
5 changes: 5 additions & 0 deletions .changeset/polite-donkeys-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

avoid setting rawBody/body to an empty Uint8Array when a load's fetch function is called with no body during SSR
13 changes: 13 additions & 0 deletions .changeset/pre.json
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@
"big-garlics-complain",
"blue-apes-type",
"blue-bees-jump",
"blue-bulldogs-visit",
"blue-cats-mate",
"blue-llamas-exist",
"blue-poets-jam",
@@ -98,6 +99,7 @@
"eighty-candles-grow",
"eighty-carrots-heal",
"eighty-waves-obey",
"empty-donuts-smell",
"famous-rules-sort",
"famous-turtles-appear",
"fast-cameras-bow",
@@ -106,6 +108,7 @@
"few-points-taste",
"few-rockets-think",
"few-singers-pump",
"few-suits-drive",
"few-suits-flash",
"fifty-turtles-joke",
"five-shirts-allow",
@@ -119,6 +122,7 @@
"forty-months-prove",
"four-cheetahs-yawn",
"four-pillows-give",
"fresh-adults-ring",
"fresh-hats-tie",
"friendly-rivers-argue",
"funny-needles-talk",
@@ -237,6 +241,7 @@
"nice-bags-bathe",
"nice-ways-search",
"nine-actors-whisper",
"nine-beans-rhyme",
"nine-rats-heal",
"nine-starfishes-destroy",
"nine-tomatoes-poke",
@@ -255,6 +260,7 @@
"plenty-cougars-invite",
"plenty-donuts-check",
"polite-apes-relax",
"polite-donkeys-learn",
"polite-ducks-notice",
"polite-masks-dress",
"polite-worms-happen",
@@ -280,6 +286,7 @@
"quick-maps-remain",
"quiet-ants-report",
"quiet-cherries-smile",
"quiet-crews-hunt",
"quiet-mangos-shop",
"quiet-mugs-matter",
"quiet-singers-fly",
@@ -318,6 +325,7 @@
"shiny-teachers-nail",
"short-beds-punch",
"short-buttons-mix",
"short-emus-juggle",
"shy-jeans-fly",
"shy-mails-share",
"silly-grapes-cover",
@@ -335,6 +343,7 @@
"sixty-terms-decide",
"slow-buses-beam",
"slow-fans-own",
"small-ligers-wave",
"small-pigs-doubt",
"smart-bears-leave",
"smart-humans-eat",
@@ -353,6 +362,7 @@
"soft-houses-pump",
"soft-news-teach",
"soft-students-cover",
"sour-waves-return",
"spicy-buckets-draw",
"spicy-geese-accept",
"spicy-jeans-rest",
@@ -389,6 +399,7 @@
"thick-peas-shop",
"thick-steaks-sneeze",
"thin-avocados-visit",
"thin-bats-protect",
"thin-coins-move",
"thin-grapes-dance",
"thin-planets-obey",
@@ -425,6 +436,7 @@
"twelve-goats-knock",
"twelve-onions-burn",
"twenty-dryers-hope",
"twenty-hairs-shave",
"two-crabs-tease",
"two-students-melt",
"two-ties-begin",
@@ -462,6 +474,7 @@
"wise-bugs-run",
"witty-eyes-relax",
"witty-ligers-listen",
"witty-maps-pull",
"yellow-meals-agree",
"yellow-steaks-doubt",
"yellow-terms-accept",
5 changes: 5 additions & 0 deletions .changeset/quiet-crews-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-cloudflare-workers': patch
---

Support assigning multiple values to a header
5 changes: 5 additions & 0 deletions .changeset/short-emus-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Add `@sveltejs/kit` to noExternal to resolve hooks module in dev server
5 changes: 5 additions & 0 deletions .changeset/small-ligers-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[fix] remove Vite workaround now that dev deps can be bundled
5 changes: 5 additions & 0 deletions .changeset/sour-waves-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

support using arrays for kit.vite.resolve.alias
5 changes: 5 additions & 0 deletions .changeset/thin-bats-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[fix] HMR on Windows
5 changes: 5 additions & 0 deletions .changeset/twenty-hairs-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Enable nested dependency optimization by updating to @sveltejs/[email protected]
5 changes: 5 additions & 0 deletions .changeset/witty-maps-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Add `@sveltejs/kit` to `noExternal` in dev server as well
23 changes: 17 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -20,6 +20,15 @@ Please see [the documentation](https://kit.svelte.dev/docs) for information abou

The SvelteKit community also makes additional [SvelteKit adapters available for use](https://sveltesociety.dev/components#category-SvelteKit%20Adapters).

## Bug reporting

Please make sure the issue you're reporting involves SvelteKit. Many issues related to how a project builds originate from [Vite](https://vitejs.dev/), which SvelteKit uses to build a project. It's important to note that new Vite projects don't use SSR by default and so if you create a new Vite project from scratch many issues won't reproduce eventhough they're caused by Vite. You should thus start with a project that utilizes SSR such as:

- https://github.com/GrygrFlzr/vite-ssr-d3
- https://github.com/sveltejs/vite-plugin-svelte/tree/main/packages/e2e-tests/vite-ssr

If an issue is caused by Vite, please report in the [Vite issue tracker](https://github.com/vitejs/vite/issues).

## Developing

This monorepo uses [pnpm](https://pnpm.js.org/en/). Install it...
@@ -94,11 +103,13 @@ New packages will need to be published manually the first time if they are scope
npm publish --access=public
```

## Bug reporting
## Code structure

Please make sure the issue you're reporting involves SvelteKit. Many issues related to how a project builds originate from [Vite](https://vitejs.dev/), which SvelteKit uses to build a project. It's important to note that new Vite projects don't use SSR by default and so if you create a new Vite project from scratch many issues won't reproduce eventhough they're caused by Vite. You should thus start with a project that utilizes SSR such as:
Entry points to be aware of are:
- [`packages/create-svelte`](https://github.com/sveltejs/kit/tree/master/packages/create-svelte) - code that's run when you create a new project with `npm init svelte@next`
- [`packages/kit/src/packaging`](https://github.com/sveltejs/kit/tree/master/packages/kit/src/packaging) - for the `svelte-kit package` command
- [`packages/kit/src/core/dev/index.js`](https://github.com/sveltejs/kit/blob/master/packages/kit/src/core/dev/index.js) - for the dev-mode server
- [`packages/kit/src/core/build/index.js`](https://github.com/sveltejs/kit/blob/master/packages/kit/src/core/build/index.js) - for the production server
- [`packages/adapter-[platform]`](https://github.com/sveltejs/kit/tree/master/packages) - for the various SvelteKit-provided adapters

- https://github.com/GrygrFlzr/vite-ssr-d3
- https://github.com/sveltejs/vite-plugin-svelte/tree/main/packages/e2e-tests/vite-ssr

If an issue is caused by Vite, please report in the [Vite issue tracker](https://github.com/vitejs/vite/issues).
Most code that's called at build-time or from the CLI entry point lives in [packages/kit/src/core](https://github.com/sveltejs/kit/tree/master/packages/kit/src/core). Code that runs for rendering and routing lives in [packages/kit/src/runtime](https://github.com/sveltejs/kit/tree/master/packages/kit/src/runtime). Most changes to SvelteKit itself would involve code in these two directories.
47 changes: 32 additions & 15 deletions documentation/docs/01-routing.md
Original file line number Diff line number Diff line change
@@ -48,36 +48,52 @@ A file or directory can have multiple dynamic parts, like `[id]-[category].svelt
Endpoints are modules written in `.js` (or `.ts`) files that export functions corresponding to HTTP methods.

```ts
// Endpoint TypeScript type definitions
// Declaration types for Endpoints
// * declarations that are not exported are for internal use

type Headers = Record<string, string>;
type DefaultBody = JSONValue | Uint8Array;
// type of string[] is only for set-cookie
// everything else must be a type of string
type ResponseHeaders = Record<string, string | string[]>;
type RequestHeaders = Record<string, string>;

type Request<Locals = Record<string, any>, Body = unknown> = {
export type RawBody = null | Uint8Array;
export interface IncomingRequest {
method: string;
host: string;
headers: Headers;
path: string;
params: Record<string, string>;
query: URLSearchParams;
rawBody: Uint8Array;
headers: RequestHeaders;
rawBody: RawBody;
}

type ParameterizedBody<Body = unknown> = Body extends FormData
? ReadOnlyFormData
: (string | RawBody | ReadOnlyFormData) & Body;
// ServerRequest is exported as Request
export interface ServerRequest<Locals = Record<string, any>, Body = unknown>
extends IncomingRequest {
params: Record<string, string>;
body: ParameterizedBody<Body>;
locals: Locals; // populated by hooks handle
};
}

type EndpointOutput<Body extends DefaultBody = DefaultBody> = {
type DefaultBody = JSONResponse | Uint8Array;
export interface EndpointOutput<Body extends DefaultBody = DefaultBody> {
status?: number;
headers?: Headers;
headers?: ResponseHeaders;
body?: Body;
};
}

type RequestHandler<
export interface RequestHandler<
Locals = Record<string, any>,
Input = unknown,
Output extends DefaultBody = DefaultBody
> = (
request: Request<Locals, Input>
) => void | EndpointOutput<Output> | Promise<void | EndpointOutput<Output>>;
> {
(request: ServerRequest<Locals, Input>):
| void
| EndpointOutput<Output>
| Promise<void | EndpointOutput<Output>>;
}
```

For example, our hypothetical blog page, `/blog/cool-article`, might request data from `/blog/cool-article.json`, which could be represented by a `src/routes/blog/[slug].json.js` endpoint:
@@ -143,6 +159,7 @@ return {
#### Body parsing

The `body` property of the request object will be provided in the case of POST requests:

- Text data (with content-type `text/plain`) will be parsed to a `string`
- JSON data (with content-type `application/json`) will be parsed to a `JSONValue` (an `object`, `Array`, or primitive).
- Form data (with content-type `application/x-www-form-urlencoded` or `multipart/form-data`) will be parsed to a read-only version of the [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData) object.
21 changes: 12 additions & 9 deletions documentation/docs/03-loading.md
Original file line number Diff line number Diff line change
@@ -5,35 +5,36 @@ title: Loading
A component that defines a page or a layout can export a `load` function that runs before the component is created. This function runs both during server-side rendering and in the client, and allows you to get data for a page without (for example) showing a loading spinner and fetching data in `onMount`.

```ts
// load TypeScript type definitions
// Declaration types for Loading
// * declarations that are not exported are for internal use

type LoadInput<
export interface LoadInput<
PageParams extends Record<string, string> = Record<string, string>,
Context extends Record<string, any> = Record<string, any>,
Session = any
> = {
> {
page: {
host: string;
path: string;
params: PageParams;
query: URLSearchParams;
};
fetch: (info: RequestInfo, init?: RequestInit) => Promise<Response>;
fetch(info: RequestInfo, init?: RequestInit): Promise<Response>;
session: Session;
context: Context;
};
}

type LoadOutput<
export interface LoadOutput<
Props extends Record<string, any> = Record<string, any>,
Context extends Record<string, any> = Record<string, any>
> = {
> {
status?: number;
error?: string | Error;
redirect?: string;
props?: Props;
context?: Context;
maxage?: number;
};
}
```

Our example blog page might contain a `load` function like the following:
@@ -62,13 +63,15 @@ Our example blog page might contain a `load` function like the following:
}
</script>
```
> Note the `<script context="module">` — this is necessary because `load` runs before the component is rendered. Code that is per-component instance should go into a second `<script>` tag.

> Note the `<script context="module">` — this is necessary because `load` runs before the component is rendered. Code that is per-component instance should go into a second `<script>` tag.

`load` is similar to `getStaticProps` or `getServerSideProps` in Next.js, except that it runs on both the server and the client.

If `load` returns nothing, SvelteKit will [fall through](#routing-advanced-fallthrough-routes) to other routes until something responds, or will respond with a generic 404.

SvelteKit's `load` receives an implemention of `fetch`, which has the following special properties:

- it has access to cookies on the server
- it can make requests against the app's own endpoints without issuing an HTTP call
- it makes a copy of the response when you use it, and then sends it embedded in the initial page load for hydration
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.