-
Notifications
You must be signed in to change notification settings - Fork 103
feat: add spa_fallback config option #7141
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
Changes from 7 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a25c907
add build.spa in netlify.toml
hrishikesh-k 1c3b2de
add build.spa in frameworks api as well
hrishikesh-k c0ffd12
add fallback rewrite
hrishikesh-k e9cccc2
fix format
hrishikesh-k 35af900
Merge branch 'main' into hk/spa-rewrite
hrishikesh-k 4467f3a
fix tests 1
hrishikesh-k f5b1e16
Merge branch 'main' into hk/spa-rewrite
hrishikesh-k ee1a763
Merge branch 'main' into hk/spa-rewrite
hrishikesh-k 81340da
change test to vitest and disable supression
hrishikesh-k 9b2009f
add warning for invalid rewrite
hrishikesh-k d623053
coderabbit 1
hrishikesh-k b7ae7e4
fix fmt
hrishikesh-k 9bc662c
rename build.spa to spa_fallback
hrishikesh-k d82de31
Merge branch 'main' into hk/spa-rewrite
hrishikesh-k 9d95492
coderabbit test added
hrishikesh-k 0a2293c
Merge branch 'main' into hk/spa-rewrite
serhalp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import type { NetlifyConfig } from '../../index.js' | ||
| import { getConfigMutations } from '../../plugins/child/diff.js' | ||
| import { CoreStep, CoreStepFunction, CoreStepFunctionArgs } from '../types.js' | ||
|
|
||
| // The catch-all redirect that makes a single-page application's client-side | ||
| // router handle every path. | ||
| const SPA_FALLBACK_REDIRECT = { | ||
| from: '/*', | ||
| status: 200, | ||
| to: '/index.html', | ||
| } | ||
|
|
||
| function hasCatchAllRedirect(redirects: NetlifyConfig['redirects']) { | ||
| return redirects.some((r) => r.from === '/*') | ||
| } | ||
|
|
||
| function coreStep(coreStepFunctionArgs: CoreStepFunctionArgs): ReturnType<CoreStepFunction> { | ||
| if ( | ||
| !coreStepFunctionArgs.netlifyConfig.build.spa || | ||
| hasCatchAllRedirect(coreStepFunctionArgs.netlifyConfig.redirects) | ||
| ) { | ||
| return Promise.resolve({}) | ||
| } | ||
|
serhalp marked this conversation as resolved.
Outdated
|
||
|
|
||
| const newConfig: Partial<NetlifyConfig> = { | ||
| redirects: [...coreStepFunctionArgs.netlifyConfig.redirects, SPA_FALLBACK_REDIRECT], | ||
| } | ||
|
|
||
| const configMutations = getConfigMutations( | ||
| coreStepFunctionArgs.netlifyConfig, | ||
| { | ||
| ...coreStepFunctionArgs.netlifyConfig, | ||
| ...newConfig, | ||
| }, | ||
| applySpaFallback.event, | ||
| ) as unknown[] | ||
|
|
||
| return Promise.resolve({ configMutations }) | ||
| } | ||
|
|
||
| export const applySpaFallback: CoreStep = { | ||
| coreStep, | ||
| coreStepDescription: () => '', | ||
| coreStepId: 'spa_fallback', | ||
| coreStepName: 'Applying SPA fallback redirect', | ||
| event: 'onPostBuild', | ||
| quiet: true, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
2 changes: 2 additions & 0 deletions
2
packages/build/tests/frameworks_api/fixtures/spa_config/.gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # .netlify/v1/config.json is generated at build time by build.mjs. | ||
| .netlify |
11 changes: 11 additions & 0 deletions
11
packages/build/tests/frameworks_api/fixtures/spa_config/build.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { mkdir, writeFile } from 'node:fs/promises' | ||
|
|
||
| const config = { | ||
| build: { | ||
| spa: true, | ||
| }, | ||
| } | ||
|
|
||
| await mkdir('.netlify/v1', { recursive: true }) | ||
|
|
||
| await writeFile('.netlify/v1/config.json', JSON.stringify(config)) |
2 changes: 2 additions & 0 deletions
2
packages/build/tests/frameworks_api/fixtures/spa_config/netlify.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| [build] | ||
| command = "node build.mjs" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
packages/build/tests/spa_fallback/fixtures/spa_default/netlify.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| [build] | ||
| command = "echo hi" |
3 changes: 3 additions & 0 deletions
3
packages/build/tests/spa_fallback/fixtures/spa_disabled/netlify.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| [build] | ||
| command = "echo hi" | ||
| spa = false |
3 changes: 3 additions & 0 deletions
3
packages/build/tests/spa_fallback/fixtures/spa_enabled/netlify.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| [build] | ||
| command = "echo hi" | ||
| spa = true |
8 changes: 8 additions & 0 deletions
8
packages/build/tests/spa_fallback/fixtures/spa_enabled_existing_catch_all/netlify.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| [build] | ||
| command = "echo hi" | ||
| spa = true | ||
|
|
||
| [[redirects]] | ||
| from = "/*" | ||
| to = "/200.html" | ||
| status = 200 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { Fixture } from '@netlify/testing' | ||
| import test from 'ava' | ||
|
serhalp marked this conversation as resolved.
Outdated
|
||
|
|
||
| const SPA_FALLBACK_REDIRECT = { | ||
| conditions: {}, | ||
| headers: {}, | ||
| force: false, | ||
| from: '/*', | ||
| query: {}, | ||
| status: 200, | ||
| to: '/index.html', | ||
| } | ||
|
|
||
| test('Injects an SPA fallback redirect when `build.spa` is `true`', async (t) => { | ||
| const { netlifyConfig, success } = await new Fixture( | ||
| test.meta.file, | ||
| './fixtures/spa_enabled', | ||
| ).runWithBuildAndIntrospect() | ||
|
|
||
| t.true(success) | ||
| t.deepEqual(netlifyConfig.redirects, [SPA_FALLBACK_REDIRECT]) | ||
| }) | ||
|
|
||
| test('Does not inject an SPA fallback redirect when `build.spa` is `false`', async (t) => { | ||
| const { netlifyConfig, success } = await new Fixture( | ||
| test.meta.file, | ||
| './fixtures/spa_disabled', | ||
| ).runWithBuildAndIntrospect() | ||
|
|
||
| t.true(success) | ||
| t.deepEqual(netlifyConfig.redirects, []) | ||
| }) | ||
|
|
||
| test('Does not inject an SPA fallback redirect when `build.spa` is not set', async (t) => { | ||
| const { netlifyConfig, success } = await new Fixture( | ||
| test.meta.file, | ||
| './fixtures/spa_default', | ||
| ).runWithBuildAndIntrospect() | ||
|
|
||
| t.true(success) | ||
| t.deepEqual(netlifyConfig.redirects, []) | ||
| }) | ||
|
|
||
| test('Does not override a catch-all redirect already declared by the user', async (t) => { | ||
| const { netlifyConfig, success } = await new Fixture( | ||
| test.meta.file, | ||
| './fixtures/spa_enabled_existing_catch_all', | ||
| ).runWithBuildAndIntrospect() | ||
|
|
||
| t.true(success) | ||
| t.deepEqual(netlifyConfig.redirects, [{ ...SPA_FALLBACK_REDIRECT, to: '/200.html' }]) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.