Skip to content

Commit

Permalink
fix: clear redirects/rewrites produced by previous builds and generat…
Browse files Browse the repository at this point in the history
…e functions in ntl dev (#738)

* chore: unpin netlify-cli used for tests

* fix: clear redirects/rewrites produced by previous builds in ntl dev

* test: adjust content-type assertions

* fix: create Netlify Functions in dev

* Revert "test: adjust content-type assertions"

This reverts commit fe67398.
  • Loading branch information
pieh authored Jan 10, 2024
1 parent 2243f3a commit e66731c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 33 additions & 6 deletions plugin/src/helpers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,20 +200,47 @@ export async function modifyConfig({
netlifyConfig,
cacheDir,
neededFunctions,
isDev,
}: {
netlifyConfig: NetlifyConfig
cacheDir: string
neededFunctions: FunctionList
isDev?: boolean
}): Promise<void> {
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,
})
}
}
Expand Down
24 changes: 23 additions & 1 deletion plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ export async function onPreBuild({
await checkNetlifyImageCdn({ netlifyConfig })
}

export async function onDev({
netlifyConfig,
constants,
}: NetlifyPluginOptions): Promise<void> {
// 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,
Expand Down Expand Up @@ -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 })
}
Expand Down

0 comments on commit e66731c

Please sign in to comment.