-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27050 from storybookjs/yann/nextjs-redirect-boundary
Nextjs: Implement next redirect and the RedirectBoundary
- Loading branch information
Showing
16 changed files
with
456 additions
and
1,285 deletions.
There are no files selected for viewing
This file contains 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 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
32 changes: 32 additions & 0 deletions
32
code/frameworks/nextjs/src/compatibility/compatibility-map.ts
This file contains 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,32 @@ | ||
import type { Configuration as WebpackConfig } from 'webpack'; | ||
import semver from 'semver'; | ||
import { getNextjsVersion, addScopedAlias } from '../utils'; | ||
|
||
const mapping: Record<string, Record<string, string>> = { | ||
'<14.0.0': { | ||
'next/dist/shared/lib/segment': '@storybook/nextjs/dist/compatibility/segment.compat', | ||
'next/dist/client/components/redirect-status-code': | ||
'@storybook/nextjs/dist/compatibility/redirect-status-code.compat', | ||
}, | ||
}; | ||
|
||
export const getCompatibilityAliases = () => { | ||
const version = getNextjsVersion(); | ||
const result: Record<string, string> = {}; | ||
|
||
Object.keys(mapping).filter((key) => { | ||
if (semver.intersects(version, key)) { | ||
Object.assign(result, mapping[key]); | ||
} | ||
}); | ||
|
||
return result; | ||
}; | ||
|
||
export const configureCompatibilityAliases = (baseConfig: WebpackConfig): void => { | ||
const aliases = getCompatibilityAliases(); | ||
|
||
Object.entries(aliases).forEach(([name, alias]) => { | ||
addScopedAlias(baseConfig, name, alias); | ||
}); | ||
}; |
6 changes: 6 additions & 0 deletions
6
code/frameworks/nextjs/src/compatibility/redirect-status-code.compat.ts
This file contains 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,6 @@ | ||
// Compatibility for Next 13 | ||
export enum RedirectStatusCode { | ||
SeeOther = 303, | ||
TemporaryRedirect = 307, | ||
PermanentRedirect = 308, | ||
} |
This file contains 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 @@ | ||
// Compatibility for Next 13 | ||
// from https://github.com/vercel/next.js/blob/606f9ff7903b58da51aa043bfe71cd7b6ea306fd/packages/next/src/shared/lib/segment.ts#L4 | ||
export function isGroupSegment(segment: string) { | ||
return segment[0] === '(' && segment.endsWith(')'); | ||
} | ||
|
||
export const PAGE_SEGMENT_KEY = '__PAGE__'; | ||
export const DEFAULT_SEGMENT_KEY = '__DEFAULT__'; |
This file contains 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 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 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 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 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 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
54 changes: 54 additions & 0 deletions
54
code/frameworks/nextjs/template/stories_nextjs-default-ts/Redirect.stories.tsx
This file contains 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,54 @@ | ||
import React from 'react'; | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import { userEvent, within } from '@storybook/test'; | ||
import { redirect } from 'next/navigation'; | ||
|
||
let state = 'Bug! Not invalidated'; | ||
|
||
export default { | ||
render() { | ||
return ( | ||
<div> | ||
<div>{state}</div> | ||
<form | ||
action={() => { | ||
state = 'State is invalidated successfully.'; | ||
redirect('/'); | ||
}} | ||
> | ||
<button>Submit</button> | ||
</form> | ||
</div> | ||
); | ||
}, | ||
parameters: { | ||
test: { | ||
// This is needed until Next will update to the React 19 beta: https://github.com/vercel/next.js/pull/65058 | ||
// In the React 19 beta ErrorBoundary errors (such as redirect) are only logged, and not thrown. | ||
// We will also suspress console.error logs for re the console.error logs for redirect in the next framework. | ||
// Using the onCaughtError react root option: | ||
// react: { | ||
// rootOptions: { | ||
// onCaughtError(error: unknown) { | ||
// if (isNextRouterError(error)) return; | ||
// console.error(error); | ||
// }, | ||
// }, | ||
// See: code/frameworks/nextjs/src/preview.tsx | ||
dangerouslyIgnoreUnhandledErrors: true, | ||
}, | ||
nextjs: { | ||
appDirectory: true, | ||
navigation: { | ||
pathname: '/', | ||
}, | ||
}, | ||
}, | ||
} as Meta; | ||
|
||
export const SingletonStateGetsInvalidatedAfterRedirecting: StoryObj = { | ||
play: async ({ canvasElement, step }) => { | ||
const canvas = within(canvasElement); | ||
await userEvent.click(canvas.getByRole('button')); | ||
}, | ||
}; |
Oops, something went wrong.