Skip to content

fix: clear redirects/rewrites produced by previous builds and generate functions in ntl dev #738

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

Merged
merged 5 commits into from
Jan 10, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
: '',
Comment on lines +217 to +219
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this now always handles redirects - if API is "not needed" - it will clean up dangling redirect

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`
Copy link
Contributor Author

@pieh pieh Jan 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gatsby compiles functions lazily in dev (waiting for /api/X to be requested before function is compiled) by default which cause problems if we want to wrap them - this will force compile them eagerly


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