Skip to content

Commit a8863d0

Browse files
committed
Chnages from review
1 parent bb5939d commit a8863d0

File tree

3 files changed

+42
-17
lines changed

3 files changed

+42
-17
lines changed

examples/hn.svelte.dev/netlify.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[build]
22
command = "npm run build"
3-
publish = ".svelte-kit/netlify/build/"
3+
publish = "build"
44

55
[build.environment]
66
NPM_FLAGS="--prefix=/dev/null"

packages/adapter-netlify/README.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@ export default {
2727
};
2828
```
2929

30-
Then, make sure you have a [netlify.toml](https://docs.netlify.com/configure-builds/file-based-configuration) file in the project root. This will determine where to write static assets and functions to based on the `build.publish` settings, as per this sample configuration:
30+
Then, make sure you have a [netlify.toml](https://docs.netlify.com/configure-builds/file-based-configuration) file in the project root. This will determine where to write static assets based on the `build.publish` settings, as per this sample configuration:
3131

3232
```toml
3333
[build]
3434
command = "npm run build"
35-
publish = "build/publish/"
35+
publish = "build"
3636
```
3737

38+
If the `netlify.toml` file or the `build.publish` value is missing, a default value of `"build"` will be used. Note that if you have set the publish directory in the Netlify UI to something else then you will need to set it in `netlify.toml` too, or use the default value of `"build"`.
39+
3840
## Netlify alternatives to SvelteKit functionality
3941

4042
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.
@@ -52,6 +54,20 @@ During compilation a required "catch all" redirect rule is automatically appende
5254
2. Netlify's build bot parses your HTML files at deploy time, which means your form must be [prerendered](https://kit.svelte.dev/docs#ssr-and-javascript-prerender) as HTML. You can either add `export const prerender = true` to your `contact.svelte` to prerender just that page or set the `kit.prerender.force: true` option to prerender all pages.
5355
3. If your Netlify form has a [custom success message](https://docs.netlify.com/forms/setup/#success-messages) like `<form netlify ... action="/success">` then ensure the corresponding `/routes/success.svelte` exists and is prerendered.
5456

57+
### Using Netlify Functions
58+
59+
[Netlify Functions](https://docs.netlify.com/functions/overview/) can be used alongside your SvelteKit routes. If you would like to add them to your site, you should create a directory for them and add the configuration to your `netlify.toml` file. For example:
60+
61+
```toml
62+
[build]
63+
command = "npm run build"
64+
publish = "build"
65+
66+
[functions]
67+
directory = "functions"
68+
node_bundler = "esbuild"
69+
```
70+
5571
## Advanced Configuration
5672

5773
### esbuild
@@ -76,7 +92,8 @@ The default options for this version are as follows:
7692
```js
7793
{
7894
entryPoints: ['.svelte-kit/netlify/entry.js'],
79-
outfile: `pathToFunctionsFolder/render/index.js`,
95+
// This is Netlify's internal functions directory, not the one for user functions.
96+
outfile: '.netlify/functions-internal/__render.js',
8097
bundle: true,
8198
inject: ['pathTo/shims.js'],
8299
platform: 'node'

packages/adapter-netlify/index.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { appendFileSync, existsSync, readFileSync, writeFileSync } from 'fs';
2-
import { join } from 'path';
2+
import { join, resolve } from 'path';
33
import { fileURLToPath } from 'url';
44
import esbuild from 'esbuild';
55
import toml from '@iarna/toml';
@@ -19,7 +19,9 @@ export default function (options) {
1919
name: '@sveltejs/adapter-netlify',
2020

2121
async adapt({ utils }) {
22-
const { publish } = validate_config().build;
22+
const publish = get_publish_directory(utils) || 'build';
23+
24+
utils.log.minor(`Publishing to "${publish}"`);
2325

2426
utils.rimraf(publish);
2527

@@ -46,7 +48,7 @@ export default function (options) {
4648

4749
writeFileSync(join('.netlify', 'package.json'), JSON.stringify({ type: 'commonjs' }));
4850

49-
utils.log.info('Prerendering static pages...');
51+
utils.log.minor('Prerendering static pages...');
5052
await utils.prerender({
5153
dest: publish
5254
});
@@ -65,9 +67,12 @@ export default function (options) {
6567

6668
return adapter;
6769
}
68-
69-
function validate_config() {
70+
/**
71+
* @param {import('@sveltejs/kit').AdapterUtils} utils
72+
**/
73+
function get_publish_directory(utils) {
7074
if (existsSync('netlify.toml')) {
75+
/** @type {{ build?: { publish?: string }} & toml.JsonMap } */
7176
let netlify_config;
7277

7378
try {
@@ -78,22 +83,25 @@ function validate_config() {
7883
}
7984

8085
if (!netlify_config.build || !netlify_config.build.publish) {
81-
throw new Error(
82-
'You must specify build.publish in netlify.toml. Consult https://github.com/sveltejs/kit/tree/master/packages/adapter-netlify#configuration'
83-
);
86+
// This is the default publish directory when Netlify detects SvelteKit
87+
utils.log.warn('No publish directory specified in netlify.toml, using default');
88+
return;
8489
}
8590

8691
if (netlify_config.redirects) {
8792
throw new Error(
8893
"Redirects are not supported in netlify.toml. Use _redirects instead. For more details consult the readme's troubleshooting section."
8994
);
9095
}
91-
92-
return netlify_config;
96+
if (resolve(netlify_config.build.publish) === process.cwd()) {
97+
throw new Error(
98+
'The publish directory cannot be set to the site root. Please change it to "build" in netlify.toml.'
99+
);
100+
}
101+
return netlify_config.build.publish;
93102
}
94103

95-
// TODO offer to create one?
96-
throw new Error(
97-
'Missing a netlify.toml file. Consult https://github.com/sveltejs/kit/tree/master/packages/adapter-netlify#configuration'
104+
utils.log.warn(
105+
'No netlify.toml found. Using default publish directory. Consult https://github.com/sveltejs/kit/tree/master/packages/adapter-netlify#configuration for more details '
98106
);
99107
}

0 commit comments

Comments
 (0)