diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5122e5bb..f89978b8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -29,7 +29,7 @@ jobs: key: ubuntu-build-${{ env.cache-name }}-${{ hashFiles('plugin/test/fixtures/**/package.json') }}-node-modules - - run: npm install -g netlify-cli@17.6.0 + - run: npm install -g netlify-cli - run: npm ci - run: cd plugin && npm ci && npm run build - run: npm test diff --git a/plugin/src/helpers/config.ts b/plugin/src/helpers/config.ts index 43aab3bd..6cdbe779 100644 --- a/plugin/src/helpers/config.ts +++ b/plugin/src/helpers/config.ts @@ -200,20 +200,47 @@ export async function modifyConfig({ netlifyConfig, cacheDir, neededFunctions, + isDev, }: { netlifyConfig: NetlifyConfig cacheDir: string neededFunctions: FunctionList + isDev?: boolean }): Promise { mutateConfig({ netlifyConfig, cacheDir, neededFunctions }) - if (neededFunctions.includes('API')) { - // Editing _redirects so it works with ntl dev + const redirectsFileName = join(netlifyConfig.build.publish, '_redirects') + + await spliceConfig({ + startMarker: '# @netlify/plugin-gatsby redirects start', + endMarker: '# @netlify/plugin-gatsby redirects end', + contents: neededFunctions.includes('API') + ? '/api/* /.netlify/functions/__api 200' + : '', + fileName: redirectsFileName, + }) + + if (isDev) { + // removing redirects that possibly were added for builds previously + // DSG/SSR is fully handled by gatsby dev server and is not even produced in dev + + // this is a bit of a hack - gatsby-plugin-netlify appends redirects before @netlify/plugin-gatsby + // so we use gatsby-plugin-netlify marker as start and @netlify/plugin-gatsby start marker as end + await spliceConfig({ + startMarker: '## Created with gatsby-plugin-netlify', + endMarker: '# @netlify/plugin-gatsby redirects start', + contents: '', + fileName: redirectsFileName, + }) + // this removes redirects produced by adapters in newer gatsby versions + // while build plugin doesn't do any work during builds when adapters are used + // adapters don't have hooks for `develop` so they can't clean their redirects + // so build plugin is handling that as it still runs (just skips most of the work) await spliceConfig({ - startMarker: '# @netlify/plugin-gatsby redirects start', - endMarker: '# @netlify/plugin-gatsby redirects end', - contents: '/api/* /.netlify/functions/__api 200', - fileName: join(netlifyConfig.build.publish, '_redirects'), + startMarker: '# gatsby-adapter-netlify start', + endMarker: '# gatsby-adapter-netlify end', + contents: '', + fileName: redirectsFileName, }) } } diff --git a/plugin/src/index.ts b/plugin/src/index.ts index 219c90e6..030bbabf 100644 --- a/plugin/src/index.ts +++ b/plugin/src/index.ts @@ -47,6 +47,28 @@ export async function onPreBuild({ await checkNetlifyImageCdn({ netlifyConfig }) } +export async function onDev({ + netlifyConfig, + constants, +}: NetlifyPluginOptions): Promise { + // eslint-disable-next-line no-param-reassign + netlifyConfig.build.environment.GATSBY_PRECOMPILE_DEVELOP_FUNCTIONS = `true` + + const { PUBLISH_DIR } = constants + + const cacheDir = normalizedCacheDir(PUBLISH_DIR) + + const neededFunctionsForBuild = await getNeededFunctions(cacheDir) + // DSG/SSR engine is not produced for dev so we are filtering them out + const neededFunctions = neededFunctionsForBuild.filter( + (neededFunction) => neededFunction !== 'DSG' && neededFunction !== 'SSR', + ) + + await writeFunctions({ constants, netlifyConfig, neededFunctions }) + + await modifyConfig({ netlifyConfig, cacheDir, neededFunctions, isDev: true }) +} + export async function onBuild({ constants, netlifyConfig, @@ -86,7 +108,7 @@ The plugin no longer uses this and it should be deleted to avoid conflicts.\n`) await writeFunctions({ constants, netlifyConfig, neededFunctions }) - await modifyConfig({ netlifyConfig, cacheDir, neededFunctions }) + await modifyConfig({ netlifyConfig, cacheDir, neededFunctions, isDev: false }) await modifyFiles({ netlifyConfig, neededFunctions }) }