-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): specific error when dev server is stopped (#7476)
* feat(core): when dev server stops, highlight this with own error boundary * feat(core): simplified, and using nested error boundary for catching dev server stop error * refactor(core): simplifying use of DevServerStatus as lazy import * refactor(core): naming refactor * feat(core): removing dev server stopped toast as unnecessary * refactor(core): using built ins from vite; making more explicit this is only for default vite * chore: moving babel vite transform to test-config package * chore: moving babel deps to test-config * chore: tidy to lock and named export for type used * fix: ignoring un-identified uses of babel presets and plugins in test-config * chore: naming of vite dev server stopped error
- Loading branch information
Showing
9 changed files
with
142 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"ignores": [ | ||
"@babel/preset-env", | ||
"@babel/preset-react", | ||
"@babel/preset-typescript", | ||
"babel-plugin-transform-vite-meta-hot" | ||
] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* eslint-disable i18next/no-literal-string -- will not support i18n in error boundaries */ | ||
import {Card, Container, Heading, Stack, Text} from '@sanity/ui' | ||
import {type ReactNode, useCallback, useEffect, useState} from 'react' | ||
import {type ViteHotContext} from 'vite/types/hot.js' | ||
|
||
const ERROR_TITLE = 'Dev server stopped' | ||
const ERROR_DESCRIPTION = | ||
'The development server has stopped. You may need to restart it to continue working.' | ||
|
||
class ViteDevServerStoppedError extends Error { | ||
ViteDevServerStoppedError: boolean | ||
|
||
constructor() { | ||
super(ERROR_TITLE) | ||
this.name = 'ViteDevServerStoppedError' | ||
this.ViteDevServerStoppedError = true | ||
} | ||
} | ||
const serverHot = import.meta.hot | ||
const isViteServer = (hot: unknown): hot is ViteHotContext => Boolean(hot) | ||
|
||
const useDetectViteDevServerStopped = () => { | ||
const [devServerStopped, setDevServerStopped] = useState(false) | ||
|
||
const markDevServerStopped = useCallback(() => setDevServerStopped(true), []) | ||
|
||
useEffect(() => { | ||
// no early return to optimize tree-shaking | ||
if (isViteServer(serverHot)) { | ||
serverHot.on('vite:ws:disconnect', markDevServerStopped) | ||
} | ||
}, [markDevServerStopped]) | ||
|
||
return {devServerStopped} | ||
} | ||
|
||
const ThrowViteServerStopped = () => { | ||
const {devServerStopped} = useDetectViteDevServerStopped() | ||
|
||
if (devServerStopped) throw new ViteDevServerStoppedError() | ||
|
||
return null | ||
} | ||
|
||
export const DetectViteDevServerStopped = (): ReactNode => | ||
isViteServer(serverHot) ? <ThrowViteServerStopped /> : null | ||
|
||
export const DevServerStoppedErrorScreen = (): ReactNode => ( | ||
<Card | ||
height="fill" | ||
overflow="auto" | ||
paddingY={[4, 5, 6, 7]} | ||
paddingX={4} | ||
sizing="border" | ||
tone="critical" | ||
> | ||
<Container width={3}> | ||
<Stack space={4}> | ||
<Heading>{ERROR_TITLE}</Heading> | ||
|
||
<Card border radius={2} overflow="auto" padding={4} tone="inherit"> | ||
<Stack space={4}> | ||
<Text size={2}>{ERROR_DESCRIPTION}</Text> | ||
</Stack> | ||
</Card> | ||
</Stack> | ||
</Container> | ||
</Card> | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.