Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Sep 5, 2022
2 parents ad0d1bd + cdb1ba9 commit c86e238
Show file tree
Hide file tree
Showing 52 changed files with 452 additions and 305 deletions.
5 changes: 5 additions & 0 deletions .changeset/blue-wombats-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-static': patch
---

[docs] more specific error message when prerendering fails
6 changes: 6 additions & 0 deletions .changeset/famous-terms-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'create-svelte': patch
'@sveltejs/kit': patch
---

Bump vite-plugin-svelte and required vite version
5 changes: 5 additions & 0 deletions .changeset/five-cooks-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[feat] Support for `$env/dynamic/*` in Vite ecosystem tools
4 changes: 4 additions & 0 deletions .changeset/pre.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"blue-poets-jam",
"blue-squids-march",
"blue-teachers-allow",
"blue-wombats-shake",
"brave-apricots-approve",
"brave-avocados-relax",
"brave-berries-join",
Expand Down Expand Up @@ -259,6 +260,7 @@
"famous-crabs-run",
"famous-donuts-whisper",
"famous-rules-sort",
"famous-terms-sniff",
"famous-turtles-appear",
"fast-cameras-bow",
"fast-coats-attack",
Expand Down Expand Up @@ -286,6 +288,7 @@
"fifty-taxis-tease",
"fifty-turtles-joke",
"five-bags-prove",
"five-cooks-hope",
"five-cows-happen",
"five-eagles-help",
"five-keys-rescue",
Expand Down Expand Up @@ -1018,6 +1021,7 @@
"spotty-dragons-wait",
"spotty-ladybugs-teach",
"spotty-parents-love",
"spotty-phones-love",
"spotty-ties-love",
"spotty-timers-fix",
"spotty-vans-tickle",
Expand Down
5 changes: 5 additions & 0 deletions .changeset/spotty-phones-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[breaking] Replace `externalFetch` with `handleFetch`
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ If you would like to test local changes to Vite or another dependency, you can b
{
// ...
"dependencies": {
"vite": "^3.0.0"
"vite": "^3.1.0"
},
"pnpm": {
"overrides": {
Expand Down
46 changes: 40 additions & 6 deletions documentation/docs/06-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Hooks
---

An optional `src/hooks.js` (or `src/hooks.ts`, or `src/hooks/index.js`) file exports three functions, all optional, that run on the server — `handle`, `handleError` and `externalFetch`.
An optional `src/hooks.js` (or `src/hooks.ts`, or `src/hooks/index.js`) file exports three functions, all optional, that run on the server — `handle`, `handleError` and `handleFetch`.

> The location of this file can be [configured](/docs/configuration) as `config.kit.files.hooks`
Expand Down Expand Up @@ -103,15 +103,29 @@ export function handleError({ error, event }) {

> `handleError` is only called for _unexpected_ errors. It is not called for errors created with the [`error`](/docs/modules#sveltejs-kit-error) function imported from `@sveltejs/kit`, as these are _expected_ errors.
### externalFetch
### handleFetch

This function allows you to modify (or replace) a `fetch` request for an external resource that happens inside a `load` function that runs on the server (or during pre-rendering).
This function allows you to modify (or replace) a `fetch` request that happens inside a `load` function that runs on the server (or during pre-rendering).

For example, your `load` function might make a request to a public URL like `https://api.yourapp.com` when the user performs a client-side navigation to the respective page, but during SSR it might make sense to hit the API directly (bypassing whatever proxies and load balancers sit between it and the public internet).
For example, you might need to include custom headers that are added by a proxy that sits in front of your app:

```js
/** @type {import('@sveltejs/kit').ExternalFetch} */
export async function externalFetch(request) {
// @errors: 2345
/** @type {import('@sveltejs/kit').HandleFetch} */
export async function handleFetch({ event, request, fetch }) {
const name = 'x-geolocation-city';
const value = event.request.headers.get(name);
request.headers.set(name, value);

return fetch(request);
}
```

Or your `load` function might make a request to a public URL like `https://api.yourapp.com` when the user performs a client-side navigation to the respective page, but during SSR it might make sense to hit the API directly (bypassing whatever proxies and load balancers sit between it and the public internet).

```js
/** @type {import('@sveltejs/kit').HandleFetch} */
export async function handleFetch({ request, fetch }) {
if (request.url.startsWith('https://api.yourapp.com/')) {
// clone the original request, but change the URL
request = new Request(
Expand All @@ -123,3 +137,23 @@ export async function externalFetch(request) {
return fetch(request);
}
```

#### Credentials

For same-origin requests, SvelteKit's `fetch` implementation will forward `cookie` and `authorization` headers unless the `credentials` option is set to `"omit"`.

For cross-origin requests, `cookie` will be included if the request URL belongs to a subdomain of the app — for example if your app is on `my-domain.com`, and your API is on `api.my-domain.com`, cookies will be included in the request.

If your app and your API are on sibling subdomains — `www.my-domain.com` and `api.my-domain.com` for example — then a cookie belonging to a common parent domain like `my-domain.com` will _not_ be included, because SvelteKit has no way to know which domain the cookie belongs to. In these cases you will need to manually include the cookie using `handleFetch`:

```js
// @errors: 2345
/** @type {import('@sveltejs/kit').HandleFetch} */
export async function handleFetch({ event, request, fetch }) {
if (request.url.startsWith('https://api.my-domain.com/')) {
request.headers.set('cookie', event.request.headers.get('cookie'));
}

return fetch(request);
}
```
2 changes: 1 addition & 1 deletion documentation/docs/11-page-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ It's likely that at least some routes of your app can be represented as a simple
export const prerender = true;
```

Alternatively, you can set `export const prerender = true` in your root `+layout` and prerender everything except pages that are explicitly marked as _not_ prerenderable:
Alternatively, you can set `export const prerender = true` in your root `+layout.js` or `+layout.server.js` and prerender everything except pages that are explicitly marked as _not_ prerenderable:

```js
/// file: +page.js/+page.server.js/+server.js
Expand Down
2 changes: 0 additions & 2 deletions documentation/docs/14-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ const config = {
prerender: {
concurrency: 1,
crawl: true,
default: false,
enabled: true,
entries: ['*'],
onError: 'fail',
Expand Down Expand Up @@ -260,7 +259,6 @@ See [Prerendering](/docs/page-options#prerender). An object containing zero or m

- `concurrency` — how many pages can be prerendered simultaneously. JS is single-threaded, but in cases where prerendering performance is network-bound (for example loading content from a remote CMS) this can speed things up by processing other tasks while waiting on the network response
- `crawl` — determines whether SvelteKit should find pages to prerender by following links from the seed page(s)
- `default` — set to `true` to prerender encountered pages not containing `export const prerender = false`
- `enabled` — set to `false` to disable prerendering altogether
- `entries` — an array of pages to prerender, or start crawling from (if `crawl: true`). The `*` string includes all non-dynamic routes (i.e. pages with no `[parameters]` )
- `onError`
Expand Down
6 changes: 6 additions & 0 deletions packages/adapter-static/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @sveltejs/adapter-static

## 1.0.0-next.42

### Patch Changes

- [docs] more specific error message when prerendering fails ([#6577](https://github.com/sveltejs/kit/pull/6577))

## 1.0.0-next.41

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default function (options) {
if (dynamic_routes.length > 0) {
const prefix = path.relative('.', builder.config.kit.files.routes);
builder.log.error(
`@sveltejs/adapter-static: all routes must be fully prerenderable (unless using the 'fallback' option — see https://github.com/sveltejs/kit/tree/master/packages/adapter-static#spa-mode). Try adding \`export const prerender = true\` to your root layout — see https://kit.svelte.dev/docs/page-options#prerender for more details`
`@sveltejs/adapter-static: all routes must be fully prerenderable (unless using the 'fallback' option — see https://github.com/sveltejs/kit/tree/master/packages/adapter-static#spa-mode). Try adding \`export const prerender = true\` to your root layout.js — see https://kit.svelte.dev/docs/page-options#prerender for more details`
);
builder.log.error(
dynamic_routes.map((id) => ` - ${path.posix.join(prefix, id)}`).join('\n')
Expand Down
4 changes: 2 additions & 2 deletions packages/adapter-static/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sveltejs/adapter-static",
"version": "1.0.0-next.41",
"version": "1.0.0-next.42",
"repository": {
"type": "git",
"url": "https://github.com/sveltejs/kit",
Expand Down Expand Up @@ -31,6 +31,6 @@
"svelte": "^3.48.0",
"typescript": "^4.8.2",
"uvu": "^0.5.3",
"vite": "^3.1.0-beta.2"
"vite": "^3.1.0"
}
}
2 changes: 1 addition & 1 deletion packages/adapter-static/test/apps/prerendered/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"devDependencies": {
"@sveltejs/kit": "workspace:*",
"svelte": "^3.48.0",
"vite": "^3.1.0-beta.2"
"vite": "^3.1.0"
},
"type": "module"
}
2 changes: 1 addition & 1 deletion packages/adapter-static/test/apps/spa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"@sveltejs/kit": "workspace:*",
"sirv-cli": "^2.0.2",
"svelte": "^3.48.0",
"vite": "^3.1.0-beta.2"
"vite": "^3.1.0"
},
"type": "module"
}
6 changes: 6 additions & 0 deletions packages/create-svelte/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# create-svelte

## 2.0.0-next.172

### Patch Changes

- Bump vite-plugin-svelte and required vite version ([#6583](https://github.com/sveltejs/kit/pull/6583))

## 2.0.0-next.171

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/create-svelte/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-svelte",
"version": "2.0.0-next.171",
"version": "2.0.0-next.172",
"repository": {
"type": "git",
"url": "https://github.com/sveltejs/kit",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"@sveltejs/adapter-auto": "next",
"@sveltejs/kit": "next",
"svelte": "^3.46.0",
"vite": "^3.1.0-beta.1"
"vite": "^3.1.0"
},
"type": "module",
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"@sveltejs/adapter-auto": "workspace:*",
"@sveltejs/kit": "workspace:*",
"svelte": "^3.44.0",
"vite": "^3.1.0-beta.1"
"vite": "^3.1.0"
},
"type": "module"
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"svelte": "^3.44.0",
"tslib": "^2.3.1",
"typescript": "^4.8.2",
"vite": "^3.1.0-beta.1"
"vite": "^3.1.0"
},
"type": "module"
}
10 changes: 10 additions & 0 deletions packages/kit/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# @sveltejs/kit

## 1.0.0-next.470

### Patch Changes

- Bump vite-plugin-svelte and required vite version ([#6583](https://github.com/sveltejs/kit/pull/6583))

* [feat] Support for `$env/dynamic/*` in Vite ecosystem tools ([#6454](https://github.com/sveltejs/kit/pull/6454))

- [breaking] Replace `externalFetch` with `handleFetch` ([#6565](https://github.com/sveltejs/kit/pull/6565))

## 1.0.0-next.469

### Patch Changes
Expand Down
8 changes: 4 additions & 4 deletions packages/kit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sveltejs/kit",
"version": "1.0.0-next.469",
"version": "1.0.0-next.470",
"repository": {
"type": "git",
"url": "https://github.com/sveltejs/kit",
Expand All @@ -10,7 +10,7 @@
"homepage": "https://kit.svelte.dev",
"type": "module",
"dependencies": {
"@sveltejs/vite-plugin-svelte": "^1.0.4",
"@sveltejs/vite-plugin-svelte": "^1.0.5",
"cookie": "^0.5.0",
"devalue": "^3.1.2",
"kleur": "^4.1.4",
Expand Down Expand Up @@ -38,11 +38,11 @@
"svelte-preprocess": "^4.10.6",
"typescript": "^4.8.2",
"uvu": "^0.5.3",
"vite": "^3.1.0-beta.2"
"vite": "^3.1.0"
},
"peerDependencies": {
"svelte": "^3.44.0",
"vite": "^3.1.0-beta.1"
"vite": "^3.1.0"
},
"bin": {
"svelte-kit": "svelte-kit.js"
Expand Down
13 changes: 11 additions & 2 deletions packages/kit/src/core/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,17 @@ export function create_static_module(id, env) {
return GENERATED_COMMENT + declarations.join('\n\n');
}

/** @param {'public' | 'private'} type */
export function create_dynamic_module(type) {
/**
* @param {'public' | 'private'} type
* @param {Record<string, string> | undefined} dev_values If in a development mode, values to pre-populate the module with.
*/
export function create_dynamic_module(type, dev_values) {
if (dev_values) {
const objectKeys = Object.entries(dev_values).map(
([k, v]) => `${JSON.stringify(k)}: ${JSON.stringify(v)}`
);
return `const env = {\n${objectKeys.join(',\n')}\n}\n\nexport { env }`;
}
return `export { env } from '${runtime_base}/env-${type}.js';`;
}

Expand Down
20 changes: 20 additions & 0 deletions packages/kit/src/core/sync/create_manifest_data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ function create_routes_and_nodes(cwd, config, fallback) {
* @param {import('types').RouteData | null} parent
*/
const walk = (depth, id, segment, parent) => {
if (/\]\[/.test(id)) {
throw new Error(`Invalid route ${id} — parameters must be separated`);
}

if (count_occurrences('[', id) !== count_occurrences(']', id)) {
throw new Error(`Invalid route ${id} — brackets are unbalanced`);
}

const { pattern, names, types } = parse_route_id(id);

const segments = id.split('/');
Expand Down Expand Up @@ -450,3 +458,15 @@ function list_files(dir) {

return files;
}

/**
* @param {string} needle
* @param {string} haystack
*/
function count_occurrences(needle, haystack) {
let count = 0;
for (let i = 0; i < haystack.length; i += 1) {
if (haystack[i] === needle) count += 1;
}
return count;
}
8 changes: 7 additions & 1 deletion packages/kit/src/exports/vite/build/build_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,16 @@ export class Server {
if (!this.options.hooks) {
const module = await import(${s(hooks)});
// TODO remove this for 1.0
if (module.externalFetch) {
throw new Error('externalFetch has been removed — use handleFetch instead. See https://github.com/sveltejs/kit/pull/6565 for details');
}
this.options.hooks = {
handle: module.handle || (({ event, resolve }) => resolve(event)),
handleError: module.handleError || (({ error }) => console.error(error.stack)),
externalFetch: module.externalFetch || fetch
handleFetch: module.handleFetch || (({ request, fetch }) => fetch(request))
};
}
}
Expand Down
Loading

0 comments on commit c86e238

Please sign in to comment.