From 9750009cc551bf50ee67007923bf798c95426108 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 14 Jun 2021 23:10:17 +0100 Subject: [PATCH] feat(nitro): support netlify zero-config deployments (#175) --- packages/nitro/src/presets/netlify.ts | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/nitro/src/presets/netlify.ts b/packages/nitro/src/presets/netlify.ts index 03586b2ac0..21f55b8b37 100644 --- a/packages/nitro/src/presets/netlify.ts +++ b/packages/nitro/src/presets/netlify.ts @@ -1,11 +1,31 @@ +import { join } from 'upath' +import { existsSync, readFile, writeFile } from 'fs-extra' +import consola from 'consola' import { extendPreset } from '../utils' -import { NitroPreset } from '../context' +import { NitroContext, NitroPreset } from '../context' import { lambda } from './lambda' export const netlify: NitroPreset = extendPreset(lambda, { output: { + dir: '{{ _nuxt.rootDir }}/netlify/functions', publicDir: '{{ _nuxt.rootDir }}/dist' }, + hooks: { + async 'nitro:compiled' (ctx: NitroContext) { + const redirectsPath = join(ctx._nuxt.rootDir, '_redirects') + let contents = '/* /.netlify/functions/server 200' + if (existsSync(redirectsPath)) { + const currentRedirects = await readFile(redirectsPath, 'utf-8') + if (currentRedirects.match(/^\/\* /m)) { + consola.info('Not adding Nitro fallback to `_redirects` (as an existing fallback was found).') + return + } + consola.info('Adding Nitro fallback to `_redirects` to handle all unmatched routes.') + contents = currentRedirects + '\n' + contents + } + await writeFile(redirectsPath, contents) + } + }, ignore: [ 'netlify.toml', '_redirects'