Skip to content

Commit

Permalink
Merge branch 'sveltejs:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
MathiasWP authored Jan 11, 2024
2 parents d59e339 + cc71b3b commit 4fc13c6
Show file tree
Hide file tree
Showing 113 changed files with 971 additions and 276 deletions.
6 changes: 5 additions & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<!-- Your PR description here -->

---

### Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
- [ ] It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
- [ ] This message body should clearly illustrate what problems it solves.
Expand All @@ -11,4 +15,4 @@

### Edits

Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.
- [ ] Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.
15 changes: 9 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ permissions:
contents: read # to fetch code (actions/checkout)

jobs:
Lint:
lint-all:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -32,7 +32,7 @@ jobs:
- run: pnpm run lint
- run: cd packages/kit && pnpm prepublishOnly && { [ "`git status --porcelain=v1`" == "" ] || (echo "Generated types have changed — please run prepublishOnly locally and commit the changes after you have reviewed them"; git diff; exit 1); }
- run: pnpm run check
Tests:
test-kit:
runs-on: ${{ matrix.os }}
timeout-minutes: 30
strategy:
Expand All @@ -57,7 +57,8 @@ jobs:
cache: pnpm
- run: pnpm install --frozen-lockfile
- run: pnpm playwright install ${{ matrix.e2e-browser }}
- run: pnpm test
- run: pnpm run sync-all
- run: pnpm test:kit
- name: Archive test results
if: failure()
shell: bash
Expand All @@ -69,7 +70,7 @@ jobs:
retention-days: 3
name: test-failure-${{ github.run_id }}-${{ matrix.os }}-${{ matrix.node-version }}-${{ matrix.e2e-browser }}
path: test-results.tar.gz
Cross-browser-test:
test-kit-cross-browser:
runs-on: ${{ matrix.os }}
timeout-minutes: 30
strategy:
Expand Down Expand Up @@ -112,6 +113,7 @@ jobs:
cache: pnpm
- run: pnpm install --frozen-lockfile
- run: pnpm playwright install ${{ matrix.e2e-browser }}
- run: pnpm run sync-all
- run: pnpm test:cross-platform:${{ matrix.mode }}
- name: Archive test results
if: failure()
Expand All @@ -124,7 +126,7 @@ jobs:
retention-days: 3
name: test-failure-cross-platform-${{ matrix.mode }}-${{ github.run_id }}-${{ matrix.os }}-${{ matrix.node-version }}-${{ matrix.e2e-browser }}
path: test-results-cross-platform-${{ matrix.mode }}.tar.gz
Test-create-svelte:
test-others:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -134,5 +136,6 @@ jobs:
node-version: 18
cache: pnpm
- run: pnpm install --frozen-lockfile
- run: pnpm playwright install chromium
- run: cd packages/kit && pnpm prepublishOnly
- run: pnpm run test:create-svelte
- run: pnpm run test:others
2 changes: 1 addition & 1 deletion documentation/docs/25-build-and-deploy/40-adapter-node.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ app.listen(3000, () => {

### Is there a hook for cleaning up before the server exits?

There's nothing built-in to SvelteKit for this, because such a cleanup hook depends highly on the execution environment you're on. For Node, you can use its built-in `process.on(..)` to implement a callback that runs before the server exits:
There's nothing built-in to SvelteKit for this, because such a cleanup hook depends highly on the execution environment you're on. For Node, you can use its built-in `process.on(...)` to implement a callback that runs before the server exits:

```js
// @errors: 2304 2580
Expand Down
2 changes: 2 additions & 0 deletions documentation/docs/25-build-and-deploy/50-adapter-static.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,5 @@ jobs:
id: deployment
uses: actions/deploy-pages@v2
```
If you're not using GitHub actions to deploy your site (for example, you're pushing the built site to its own repo), add an empty `.nojekyll` file in your `static` directory to prevent Jekyll from interfering.
38 changes: 37 additions & 1 deletion documentation/docs/30-advanced/20-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ title: Hooks

'Hooks' are app-wide functions you declare that SvelteKit will call in response to specific events, giving you fine-grained control over the framework's behaviour.

There are two hooks files, both optional:
There are three hooks files, all optional:

- `src/hooks.server.js` — your app's server hooks
- `src/hooks.client.js` — your app's client hooks
- `src/hooks.js` — your app's hooks that run on both the client and server

Code in these modules will run when the application starts up, making them useful for initializing database clients and so on.

Expand Down Expand Up @@ -232,6 +233,41 @@ During development, if an error occurs because of a syntax error in your Svelte

> Make sure that `handleError` _never_ throws an error
## Universal hooks

The following can be added to `src/hooks.js`. Universal hooks run on both server and client (not to be confused with shared hooks, which are environment-specific).

### reroute

This function runs before `handle` and allows you to change how URLs are translated into routes. The returned pathname (which defaults to `url.pathname`) is used to select the route and its parameters.

For example, you might have a `src/routes/[[lang]]/about/+page.svelte` page, which should be accessible as `/en/about` or `/de/ueber-uns` or `/fr/a-propos`. You could implement this with `reroute`:

```js
/// file: src/hooks.js
// @errors: 2345
// @errors: 2304

/** @type {Record<string, string>} */
const translated = {
'/en/about': '/en/about',
'/de/ueber-uns': '/de/about',
'/fr/a-propos': '/fr/about',
};

/** @type {import('@sveltejs/kit').Reroute} */
export function reroute({ url }) {
if (url.pathname in translated) {
return translated[url.pathname];
}
}
```

The `lang` parameter will be correctly derived from the returned pathname.

Using `reroute` will _not_ change the contents of the browser's address bar, or the value of `event.url`.


## Further reading

- [Tutorial: Hooks](https://learn.svelte.dev/tutorial/handle)
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@
"private": true,
"type": "module",
"scripts": {
"test": "pnpm test -r --filter=./packages/* --filter=!./packages/create-svelte",
"test:kit": "pnpm run --dir packages/kit test",
"test:cross-platform:dev": "pnpm run --dir packages/kit test:cross-platform:dev",
"test:cross-platform:build": "pnpm run --dir packages/kit test:cross-platform:build",
"test:vite-ecosystem-ci": "pnpm test --dir packages/kit",
"test:create-svelte": "pnpm run --dir packages/create-svelte test",
"test:others": "pnpm test -r --filter=./packages/* --filter=!./packages/kit/ --workspace-concurrency=1",
"check": "pnpm -r prepublishOnly && pnpm -r check",
"lint": "pnpm -r lint && eslint --cache --cache-location node_modules/.eslintcache 'packages/**/*.js'",
"format": "pnpm -r format",
"precommit": "pnpm format && pnpm lint",
"changeset:version": "changeset version && pnpm -r generate:version && git add --all",
"changeset:release": "changeset publish",
"start": "cd sites/kit.svelte.dev && npm run dev"
"start": "cd sites/kit.svelte.dev && npm run dev",
"build": "pnpm --filter @sveltejs/* -r build",
"sync-all": "node scripts/sync-all.js"
},
"devDependencies": {
"@changesets/cli": "^2.27.1",
Expand All @@ -33,7 +35,7 @@
"svelte": "^4.2.8",
"typescript": "^5.3.3"
},
"packageManager": "[email protected].0",
"packageManager": "[email protected].1",
"engines": {
"pnpm": "^8.0.0"
}
Expand Down
12 changes: 12 additions & 0 deletions packages/adapter-node/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# @sveltejs/adapter-node

## 3.0.0

### Major Changes

- breaking: allow any numeric value for `BODY_SIZE_LIMIT`, and interpret literally. Use `Infinity` rather than `0` for unrestricted body sizes ([#11589](https://github.com/sveltejs/kit/pull/11589))

## 2.1.1

### Patch Changes

- fix: correctly handle BODY_SIZE_LIMIT=0 ([#11574](https://github.com/sveltejs/kit/pull/11574))

## 2.1.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sveltejs/adapter-node",
"version": "2.1.0",
"version": "3.0.0",
"description": "Adapter for SvelteKit apps that generates a standalone Node server",
"repository": {
"type": "git",
Expand Down
8 changes: 7 additions & 1 deletion packages/adapter-node/src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ const address_header = env('ADDRESS_HEADER', '').toLowerCase();
const protocol_header = env('PROTOCOL_HEADER', '').toLowerCase();
const host_header = env('HOST_HEADER', 'host').toLowerCase();
const port_header = env('PORT_HEADER', '').toLowerCase();
const body_size_limit = parseInt(env('BODY_SIZE_LIMIT', '524288'));
const body_size_limit = Number(env('BODY_SIZE_LIMIT', '524288'));

if (isNaN(body_size_limit)) {
throw new Error(
`Invalid BODY_SIZE_LIMIT: '${env('BODY_SIZE_LIMIT')}'. Please provide a numeric value.`
);
}

const dir = path.dirname(fileURLToPath(import.meta.url));

Expand Down
6 changes: 6 additions & 0 deletions packages/enhanced-img/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @sveltejs/enhanced-img

## 0.1.8

### Patch Changes

- fix: correct images cache key to avoid collisions when images have same name ([#11602](https://github.com/sveltejs/kit/pull/11602))

## 0.1.7

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/enhanced-img/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sveltejs/enhanced-img",
"version": "0.1.7",
"version": "0.1.8",
"description": "Image optimization for your Svelte apps",
"repository": {
"type": "git",
Expand All @@ -14,7 +14,7 @@
"lint": "prettier --check .",
"check": "tsc",
"format": "prettier --write .",
"test": "vitest"
"test": "vitest run"
},
"files": [
"src",
Expand Down
46 changes: 21 additions & 25 deletions packages/enhanced-img/src/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,25 @@ export function image(opts) {
url += 'enhanced';

if (OPTIMIZABLE.test(url)) {
let image = images.get(url);
if (!image) {
// resolves the import so that we can build the entire picture template string and don't
// need any logic blocks
image = await resolve(opts, url, filename);
if (!image) {
const file_path = url.substring(0, url.indexOf('?'));
if (existsSync(path.resolve(opts.vite_config.publicDir, file_path))) {
throw new Error(
`Could not locate ${file_path}. Please move it to be located relative to the page in the routes directory or reference it beginning with /static/. See https://vitejs.dev/guide/assets for more details on referencing assets.`
);
}
// resolves the import so that we can build the entire picture template string and don't
// need any logic blocks
const resolved_id = (await opts.plugin_context.resolve(url, filename))?.id;
if (!resolved_id) {
const file_path = url.substring(0, url.indexOf('?'));
if (existsSync(path.resolve(opts.vite_config.publicDir, file_path))) {
throw new Error(
`Could not locate ${file_path}. See https://vitejs.dev/guide/assets for more details on referencing assets.`
`Could not locate ${file_path}. Please move it to be located relative to the page in the routes directory or reference it beginning with /static/. See https://vitejs.dev/guide/assets for more details on referencing assets.`
);
}
images.set(url, image);
throw new Error(
`Could not locate ${file_path}. See https://vitejs.dev/guide/assets for more details on referencing assets.`
);
}

let image = images.get(resolved_id);
if (!image) {
image = await process(resolved_id, opts);
images.set(resolved_id, image);
}
s.update(node.start, node.end, img_to_picture(content, node, image));
} else {
Expand Down Expand Up @@ -155,28 +157,22 @@ function is_quote(content, index) {
}

/**
* @param {string} resolved_id
* @param {{
* plugin_context: import('vite').Rollup.PluginContext
* imagetools_plugin: import('vite').Plugin
* }} opts
* @param {string} url
* @param {string | undefined} importer
* @returns {Promise<import('vite-imagetools').Picture | undefined>}
* @returns {Promise<import('vite-imagetools').Picture>}
*/
async function resolve(opts, url, importer) {
const resolved = await opts.plugin_context.resolve(url, importer);
const id = resolved?.id;
if (!id) {
return;
}
async function process(resolved_id, opts) {
if (!opts.imagetools_plugin.load) {
throw new Error('Invalid instance of vite-imagetools. Could not find load method.');
}
const hook = opts.imagetools_plugin.load;
const handler = typeof hook === 'object' ? hook.handler : hook;
const module_info = await handler.call(opts.plugin_context, id);
const module_info = await handler.call(opts.plugin_context, resolved_id);
if (!module_info) {
throw new Error(`Could not load ${id}`);
throw new Error(`Could not load ${resolved_id}`);
}
const code = typeof module_info === 'string' ? module_info : module_info.code;
return parseObject(code.replace('export default', '').replace(/;$/, '').trim());
Expand Down
38 changes: 38 additions & 0 deletions packages/kit/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
# @sveltejs/kit

## 2.3.1

### Patch Changes

- fix: amend onNavigate type ([#11599](https://github.com/sveltejs/kit/pull/11599))

- fix: better error message when peer dependency cannot be found ([#11598](https://github.com/sveltejs/kit/pull/11598))

## 2.3.0

### Minor Changes

- feat: add `reroute` hook ([#11537](https://github.com/sveltejs/kit/pull/11537))

## 2.2.2

### Patch Changes

- fix: only add nonce to `style-src` CSP directive when `unsafe-inline` is not present ([#11575](https://github.com/sveltejs/kit/pull/11575))

## 2.2.1

### Patch Changes

- feat: add CSP support for style-src-elem ([#11562](https://github.com/sveltejs/kit/pull/11562))

- fix: address CSP conflicts with sha/nonce during dev ([#11562](https://github.com/sveltejs/kit/pull/11562))

## 2.2.0

### Minor Changes

- feat: expose `$env/static/public` in service workers ([#10994](https://github.com/sveltejs/kit/pull/10994))

### Patch Changes

- fix: reload page on startup if `document.URL` contains credentials ([#11179](https://github.com/sveltejs/kit/pull/11179))

## 2.1.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sveltejs/kit",
"version": "2.1.2",
"version": "2.3.1",
"description": "The fastest way to build Svelte apps",
"repository": {
"type": "git",
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/core/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ function process_config(config, { cwd = process.cwd() } = {}) {
if (key === 'hooks') {
validated.kit.files.hooks.client = path.resolve(cwd, validated.kit.files.hooks.client);
validated.kit.files.hooks.server = path.resolve(cwd, validated.kit.files.hooks.server);
validated.kit.files.hooks.universal = path.resolve(cwd, validated.kit.files.hooks.universal);
} else {
// @ts-expect-error
validated.kit.files[key] = path.resolve(cwd, validated.kit.files[key]);
Expand Down
3 changes: 2 additions & 1 deletion packages/kit/src/core/config/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ const get_defaults = (prefix = '') => ({
assets: join(prefix, 'static'),
hooks: {
client: join(prefix, 'src/hooks.client'),
server: join(prefix, 'src/hooks.server')
server: join(prefix, 'src/hooks.server'),
universal: join(prefix, 'src/hooks')
},
lib: join(prefix, 'src/lib'),
params: join(prefix, 'src/params'),
Expand Down
Loading

0 comments on commit 4fc13c6

Please sign in to comment.