Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e66731c

Browse files
authored
fix: clear redirects/rewrites produced by previous builds and generate 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.
1 parent 2243f3a commit e66731c

File tree

3 files changed

+57
-8
lines changed

3 files changed

+57
-8
lines changed

‎.github/workflows/test.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
key:
3030
ubuntu-build-${{ env.cache-name }}-${{
3131
hashFiles('plugin/test/fixtures/**/package.json') }}-node-modules
32-
- run: npm install -g netlify-cli@17.6.0
32+
- run: npm install -g netlify-cli
3333
- run: npm ci
3434
- run: cd plugin && npm ci && npm run build
3535
- run: npm test

‎plugin/src/helpers/config.ts‎

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,20 +200,47 @@ export async function modifyConfig({
200200
netlifyConfig,
201201
cacheDir,
202202
neededFunctions,
203+
isDev,
203204
}: {
204205
netlifyConfig: NetlifyConfig
205206
cacheDir: string
206207
neededFunctions: FunctionList
208+
isDev?: boolean
207209
}): Promise<void> {
208210
mutateConfig({ netlifyConfig, cacheDir, neededFunctions })
209211

210-
if (neededFunctions.includes('API')) {
211-
// Editing _redirects so it works with ntl dev
212+
const redirectsFileName = join(netlifyConfig.build.publish, '_redirects')
213+
214+
await spliceConfig({
215+
startMarker: '# @netlify/plugin-gatsby redirects start',
216+
endMarker: '# @netlify/plugin-gatsby redirects end',
217+
contents: neededFunctions.includes('API')
218+
? '/api/* /.netlify/functions/__api 200'
219+
: '',
220+
fileName: redirectsFileName,
221+
})
222+
223+
if (isDev) {
224+
// removing redirects that possibly were added for builds previously
225+
// DSG/SSR is fully handled by gatsby dev server and is not even produced in dev
226+
227+
// this is a bit of a hack - gatsby-plugin-netlify appends redirects before @netlify/plugin-gatsby
228+
// so we use gatsby-plugin-netlify marker as start and @netlify/plugin-gatsby start marker as end
229+
await spliceConfig({
230+
startMarker: '## Created with gatsby-plugin-netlify',
231+
endMarker: '# @netlify/plugin-gatsby redirects start',
232+
contents: '',
233+
fileName: redirectsFileName,
234+
})
235+
// this removes redirects produced by adapters in newer gatsby versions
236+
// while build plugin doesn't do any work during builds when adapters are used
237+
// adapters don't have hooks for `develop` so they can't clean their redirects
238+
// so build plugin is handling that as it still runs (just skips most of the work)
212239
await spliceConfig({
213-
startMarker: '# @netlify/plugin-gatsby redirects start',
214-
endMarker: '# @netlify/plugin-gatsby redirects end',
215-
contents: '/api/* /.netlify/functions/__api 200',
216-
fileName: join(netlifyConfig.build.publish,'_redirects'),
240+
startMarker: '# gatsby-adapter-netlify start',
241+
endMarker: '# gatsby-adapter-netlify end',
242+
contents: '',
243+
fileName: redirectsFileName,
217244
})
218245
}
219246
}

‎plugin/src/index.ts‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,28 @@ export async function onPreBuild({
4747
await checkNetlifyImageCdn({ netlifyConfig })
4848
}
4949

50+
export async function onDev({
51+
netlifyConfig,
52+
constants,
53+
}: NetlifyPluginOptions): Promise<void> {
54+
// eslint-disable-next-line no-param-reassign
55+
netlifyConfig.build.environment.GATSBY_PRECOMPILE_DEVELOP_FUNCTIONS = `true`
56+
57+
const { PUBLISH_DIR } = constants
58+
59+
const cacheDir = normalizedCacheDir(PUBLISH_DIR)
60+
61+
const neededFunctionsForBuild = await getNeededFunctions(cacheDir)
62+
// DSG/SSR engine is not produced for dev so we are filtering them out
63+
const neededFunctions = neededFunctionsForBuild.filter(
64+
(neededFunction) => neededFunction !== 'DSG' && neededFunction !== 'SSR',
65+
)
66+
67+
await writeFunctions({ constants, netlifyConfig, neededFunctions })
68+
69+
await modifyConfig({ netlifyConfig, cacheDir, neededFunctions, isDev: true })
70+
}
71+
5072
export async function onBuild({
5173
constants,
5274
netlifyConfig,
@@ -86,7 +108,7 @@ The plugin no longer uses this and it should be deleted to avoid conflicts.\n`)
86108

87109
await writeFunctions({ constants, netlifyConfig, neededFunctions })
88110

89-
await modifyConfig({ netlifyConfig, cacheDir, neededFunctions })
111+
await modifyConfig({ netlifyConfig, cacheDir, neededFunctions,isDev: false })
90112

91113
await modifyFiles({ netlifyConfig, neededFunctions })
92114
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /