-
Notifications
You must be signed in to change notification settings - Fork 104
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 all 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| tsconfig*.tsbuildinfo |
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
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,72 @@ | ||
| import type { NetlifyConfig } from '../../index.js' | ||
| import { logWarning } from '../../log/logger.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 findCatchAllRedirect(redirects: NetlifyConfig['redirects']) { | ||
| return redirects.find((r) => r.from === '/*') | ||
| } | ||
|
|
||
| // A catch-all redirect only serves the single-page application the way ours | ||
| // would if it rewrites (rather than redirects) to the same destination. | ||
| function matchesSpaFallback(redirect: NonNullable<ReturnType<typeof findCatchAllRedirect>>) { | ||
| return redirect.to === SPA_FALLBACK_REDIRECT.to && redirect.status === SPA_FALLBACK_REDIRECT.status | ||
| } | ||
|
|
||
| function coreStep(coreStepFunctionArgs: CoreStepFunctionArgs): ReturnType<CoreStepFunction> { | ||
| const { netlifyConfig, logs } = coreStepFunctionArgs | ||
|
|
||
| if (!netlifyConfig.spa_fallback) { | ||
| return Promise.resolve({}) | ||
| } | ||
|
|
||
| const existingCatchAll = findCatchAllRedirect(netlifyConfig.redirects) | ||
|
|
||
| if (existingCatchAll) { | ||
| // `spa_fallback` asked us to add a catch-all redirect, but the site | ||
| // already has one. If it doesn't already do what ours would, warn the | ||
| // user instead of silently overriding their explicit configuration. | ||
| if (!matchesSpaFallback(existingCatchAll)) { | ||
| logWarning( | ||
| logs, | ||
| ` | ||
| Warning: "spa_fallback" is enabled, but a catch-all redirect ("/*") already exists that does not rewrite to "${SPA_FALLBACK_REDIRECT.to}" with a "${String(SPA_FALLBACK_REDIRECT.status)}" status, so Netlify did not add its own. | ||
| Please make sure your existing catch-all redirect correctly serves your single-page application.`, | ||
| ) | ||
| } | ||
|
hrishikesh-k marked this conversation as resolved.
|
||
|
|
||
| return Promise.resolve({}) | ||
| } | ||
|
|
||
| const newConfig: Partial<NetlifyConfig> = { | ||
| redirects: [...netlifyConfig.redirects, SPA_FALLBACK_REDIRECT], | ||
| } | ||
|
|
||
| const configMutations = getConfigMutations( | ||
| netlifyConfig, | ||
| { | ||
| ...netlifyConfig, | ||
| ...newConfig, | ||
| }, | ||
| applySpaFallback.event, | ||
| ) | ||
|
|
||
| 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
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_fallback_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 |
9 changes: 9 additions & 0 deletions
9
packages/build/tests/frameworks_api/fixtures/spa_fallback_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,9 @@ | ||
| import { mkdir, writeFile } from 'node:fs/promises' | ||
|
|
||
| const config = { | ||
| spa_fallback: 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_fallback_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" |
2 changes: 2 additions & 0 deletions
2
packages/build/tests/frameworks_api/fixtures/spa_fallback_config_precedence/.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 |
9 changes: 9 additions & 0 deletions
9
packages/build/tests/frameworks_api/fixtures/spa_fallback_config_precedence/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,9 @@ | ||
| import { mkdir, writeFile } from 'node:fs/promises' | ||
|
|
||
| const config = { | ||
| spa_fallback: true, | ||
| } | ||
|
|
||
| await mkdir('.netlify/v1', { recursive: true }) | ||
|
|
||
| await writeFile('.netlify/v1/config.json', JSON.stringify(config)) |
4 changes: 4 additions & 0 deletions
4
packages/build/tests/frameworks_api/fixtures/spa_fallback_config_precedence/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,4 @@ | ||
| spa_fallback = false | ||
|
|
||
| [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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { Fixture } from '@netlify/testing' | ||
| import { expect, test } from 'vitest' | ||
|
|
||
| test('Honors `spa_fallback` declared through the Frameworks API config file', async () => { | ||
| const { netlifyConfig, success } = await new Fixture( | ||
| import.meta.url, | ||
| './fixtures/spa_fallback_config', | ||
| ).runWithBuildAndIntrospect() | ||
|
|
||
| expect(success).toBe(true) | ||
| expect(netlifyConfig.spa_fallback).toBe(true) | ||
| }) | ||
|
|
||
| test('`netlify.toml` takes precedence over the Frameworks API for `spa_fallback`', async () => { | ||
| const { netlifyConfig, success } = await new Fixture( | ||
| import.meta.url, | ||
| './fixtures/spa_fallback_config_precedence', | ||
| ).runWithBuildAndIntrospect() | ||
|
|
||
| expect(success).toBe(true) | ||
| expect(netlifyConfig.spa_fallback).toBe(false) | ||
| }) |
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" |
4 changes: 4 additions & 0 deletions
4
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,4 @@ | ||
| spa_fallback = false | ||
|
|
||
| [build] | ||
| command = "echo hi" |
4 changes: 4 additions & 0 deletions
4
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,4 @@ | ||
| spa_fallback = true | ||
|
|
||
| [build] | ||
| command = "echo hi" |
9 changes: 9 additions & 0 deletions
9
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,9 @@ | ||
| spa_fallback = true | ||
|
|
||
| [build] | ||
| command = "echo hi" | ||
|
|
||
| [[redirects]] | ||
| from = "/*" | ||
| to = "/200.html" | ||
| status = 200 |
9 changes: 9 additions & 0 deletions
9
...ges/build/tests/spa_fallback/fixtures/spa_enabled_existing_correct_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,9 @@ | ||
| spa_fallback = true | ||
|
|
||
| [build] | ||
| command = "echo hi" | ||
|
|
||
| [[redirects]] | ||
| from = "/*" | ||
| to = "/index.html" | ||
| status = 200 |
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.