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

fix(netlify): allow writing config.json #2264

Merged
merged 3 commits into from Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/2.deploy/20.providers/netlify.md
Expand Up @@ -46,3 +46,7 @@ Make sure the publish directory is set to `dist` when creating a new project.
On-demand Builders are serverless functions used to generate web content as needed that’s automatically cached on Netlify’s Edge CDN. They enable you to build pages for your site when a user visits them for the first time and then cache them at the edge for subsequent visits.

:read-more{title="Netlify On-demand Builders" to="https://docs.netlify.com/configure-builds/on-demand-builders/"}

## Custom deploy configuration

You can provide additional deploy configuration using the `netlify` key inside `nitro.config`. It will be merged with built-in auto-generated config. Currently the only supported value is `images.remote_images`, for [configuring Netlify Image CDN](https://docs.netlify.com/image-cdn/create-integration/).
13 changes: 13 additions & 0 deletions src/presets/netlify.ts
Expand Up @@ -25,6 +25,19 @@ export const netlify = defineNitroPreset({
await writeHeaders(nitro);
await writeRedirects(nitro);

if (nitro.options.netlify) {
const configPath = join(
nitro.options.output.dir,
"../deploy/v1/config.json"
);
await fsp.mkdir(dirname(configPath), { recursive: true });
await fsp.writeFile(
configPath,
JSON.stringify(nitro.options.netlify),
"utf8"
);
}

const functionConfig = {
config: { nodeModuleFormat: "esm" },
version: 1,
Expand Down
4 changes: 3 additions & 1 deletion src/types/presets/index.ts
Expand Up @@ -2,12 +2,14 @@ import { AWSAmplifyOptions } from "./aws-amplify";
import { AzureOptions } from "./azure";
import { CloudflareOptions } from "./cloudflare";
import { FirebaseOptions } from "./firebase";
import { NetlifyOptions } from "./netlify";
import { VercelOptions } from "./vercel";

export interface PresetOptions {
awsAmplify: AWSAmplifyOptions;
azure: AzureOptions;
cloudflare: CloudflareOptions;
firebase: FirebaseOptions;
netlify: NetlifyOptions;
vercel: VercelOptions;
awsAmplify: AWSAmplifyOptions;
}
12 changes: 12 additions & 0 deletions src/types/presets/netlify.ts
@@ -0,0 +1,12 @@
/**
* Netlify options
*/
export interface NetlifyOptions {
images?: {
/**
* Permitted remote image sources. Array of regex strings.
* @see https://docs.netlify.com/image-cdn/overview/#remote-path
*/
remote_images?: string[];
};
}
19 changes: 19 additions & 0 deletions test/presets/netlify.test.ts
Expand Up @@ -11,6 +11,11 @@ describe("nitro:preset:netlify", async () => {
output: {
publicDir: resolve(getPresetTmpDir("netlify"), "dist"),
},
netlify: {
images: {
remote_images: ["https://example.com/.*"],
},
},
},
});
testNitro(
Expand Down Expand Up @@ -90,6 +95,20 @@ describe("nitro:preset:netlify", async () => {
`);
/* eslint-enable no-tabs */
});
it("should write config.json", async () => {
const config = await fsp
.readFile(resolve(ctx.outDir, "../deploy/v1/config.json"), "utf8")
.then((r) => JSON.parse(r));
expect(config).toMatchInlineSnapshot(`
{
"images": {
"remote_images": [
"https://example.com/.*",
],
},
}
`);
});
}
);
});