Skip to content

Commit da7b82e

Browse files
authored
fix: copy _headers and _redirects from project root instead of /static (#13227)
* docs * fix * fix again * add service worker link * grammar * try to fix docs deployment * copy files from project root instead of /static * Update .changeset/few-apricots-study.md * add error message to netlify adapter
1 parent 6e85831 commit da7b82e

File tree

6 files changed

+52
-6
lines changed

6 files changed

+52
-6
lines changed

.changeset/afraid-rules-fold.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-netlify': major
3+
---
4+
5+
fix: error if the `_headers` and `_redirects` files are in the `/static` directory instead of the project root

.changeset/few-apricots-study.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-cloudflare': major
3+
---
4+
5+
fix: copy the `_headers` and `_redirects` files from the project root instead of the `/static` directory

documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ For testing the build, you should use [Wrangler](https://developers.cloudflare.c
111111

112112
Functions contained in the [`/functions` directory](https://developers.cloudflare.com/pages/functions/routing/) at the project's root will _not_ be included in the deployment. Instead, functions should be implemented as [server endpoints](routing#server) in your SvelteKit app, which is compiled to a [single `_worker.js` file](https://developers.cloudflare.com/pages/functions/advanced-mode/).
113113

114-
The [`_headers`](https://developers.cloudflare.com/pages/configuration/headers/) and [`_redirects`](https://developers.cloudflare.com/pages/configuration/redirects/) files specific to Cloudflare Pages can be used for static asset responses (like images) by putting them into the `/static` folder.
114+
The [`_headers`](https://developers.cloudflare.com/pages/configuration/headers/) and [`_redirects`](https://developers.cloudflare.com/pages/configuration/redirects/) files specific to Cloudflare Pages can be used for static asset responses (like images) by putting them into the project root folder.
115115

116116
However, they will have no effect on responses dynamically rendered by SvelteKit, which should return custom headers or redirect responses from [server endpoints](routing#server) or with the [`handle`](hooks#Server-hooks-handle) hook.
117117

documentation/docs/25-build-and-deploy/80-adapter-netlify.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ export default {
7070

7171
You may build your app using functionality provided directly by SvelteKit without relying on any Netlify functionality. Using the SvelteKit versions of these features will allow them to be used in dev mode, tested with integration tests, and to work with other adapters should you ever decide to switch away from Netlify. However, in some scenarios you may find it beneficial to use the Netlify versions of these features. One example would be if you're migrating an app that's already hosted on Netlify to SvelteKit.
7272

73+
### `_headers` and `_redirects`
74+
75+
The [`_headers`](https://docs.netlify.com/routing/headers/#syntax-for-the-headers-file) and [`_redirects`](https://docs.netlify.com/routing/redirects/redirect-options/) files specific to Netlify can be used for static asset responses (like images) by putting them into the project root folder.
76+
7377
### Redirect rules
7478

7579
During compilation, redirect rules are automatically appended to your `_redirects` file. (If it doesn't exist yet, it will be created.) That means:

packages/adapter-cloudflare/index.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { existsSync, writeFileSync } from 'node:fs';
1+
import { copyFileSync, existsSync, writeFileSync } from 'node:fs';
22
import * as path from 'node:path';
33
import { fileURLToPath } from 'node:url';
44
import { getPlatformProxy } from 'wrangler';
@@ -14,6 +14,18 @@ export default function (options = {}) {
1414
);
1515
}
1616

17+
if (existsSync(`${builder.config.kit.files.assets}/_headers`)) {
18+
throw new Error(
19+
`The _headers file should be placed in the project root rather than the ${builder.config.kit.files.assets} directory`
20+
);
21+
}
22+
23+
if (existsSync(`${builder.config.kit.files.assets}/_redirects`)) {
24+
throw new Error(
25+
`The _redirects file should be placed in the project root rather than the ${builder.config.kit.files.assets} directory`
26+
);
27+
}
28+
1729
const files = fileURLToPath(new URL('./files', import.meta.url).href);
1830
const dest = builder.getBuildDirectory('cloudflare');
1931
const worker_dest = `${dest}/_worker.js`;
@@ -48,8 +60,16 @@ export default function (options = {}) {
4860
JSON.stringify(get_routes_json(builder, written_files, options.routes ?? {}), null, '\t')
4961
);
5062

63+
if (existsSync('_headers')) {
64+
copyFileSync('_headers', `${dest}/_headers`);
65+
}
66+
5167
writeFileSync(`${dest}/_headers`, generate_headers(builder.getAppPath()), { flag: 'a' });
5268

69+
if (existsSync('_redirects')) {
70+
copyFileSync('_redirects', `${dest}/_redirects`);
71+
}
72+
5373
if (builder.prerendered.redirects.size > 0) {
5474
writeFileSync(`${dest}/_redirects`, generate_redirects(builder.prerendered.redirects), {
5575
flag: 'a'

packages/adapter-netlify/index.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ export default function ({ split = false, edge = edge_set_in_env_var } = {}) {
5555
);
5656
}
5757

58+
if (existsSync(`${builder.config.kit.files.assets}/_headers`)) {
59+
throw new Error(
60+
`The _headers file should be placed in the project root rather than the ${builder.config.kit.files.assets} directory`
61+
);
62+
}
63+
64+
if (existsSync(`${builder.config.kit.files.assets}/_redirects`)) {
65+
throw new Error(
66+
`The _redirects file should be placed in the project root rather than the ${builder.config.kit.files.assets} directory`
67+
);
68+
}
69+
5870
const netlify_config = get_netlify_config();
5971

6072
// "build" is the default publish directory when Netlify detects SvelteKit
@@ -298,12 +310,12 @@ function generate_lambda_functions({ builder, publish, split }) {
298310
// so that generated redirects are appended to custom redirects
299311
// rather than replaced by them
300312
builder.log.minor('Writing redirects...');
301-
const redirect_file = join(publish, '_redirects');
313+
const redirects_file = join(publish, '_redirects');
302314
if (existsSync('_redirects')) {
303-
builder.copy('_redirects', redirect_file);
315+
builder.copy('_redirects', redirects_file);
304316
}
305-
builder.mkdirp(dirname(redirect_file));
306-
appendFileSync(redirect_file, `\n\n${redirects.join('\n')}`);
317+
builder.mkdirp(dirname(redirects_file));
318+
appendFileSync(redirects_file, `\n\n${redirects.join('\n')}`);
307319
}
308320

309321
function get_netlify_config() {

0 commit comments

Comments
 (0)