Skip to content

Commit

Permalink
fix: ctx.error with fatal options should work with the async loader f…
Browse files Browse the repository at this point in the history
…unction (#580)

Co-authored-by: Michael <michael.xu@binance.com>
  • Loading branch information
evenchange4 and michael-xu-20230331 committed Mar 20, 2024
1 parent 755d8a5 commit 1b7d597
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
10 changes: 10 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ configuring for webpack, babel, etc.

Type definitions for shuvi.

## Prerequisites

### Install cargo

```bash
$ curl https://sh.rustup.rs -sSf | sh
```

> docs: https://doc.rust-lang.org/cargo/getting-started/installation.html
## Setting Up a Local Copy

1. Clone the repo with `git clone https://github.com/liximomo/shuvi`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ export class ReactServerView implements IReactServerView {
renderApp: IReactServerView['renderApp'] = async ({ req, app, manifest }) => {
await Loadable.preloadAll();

const {
router,
appComponent: AppComponent,
setError: setAppError,
error
} = app;
const { router, appComponent: AppComponent, setError: setAppError } = app;
await router.ready;

// todo: move these into renderer
Expand All @@ -34,8 +29,17 @@ export class ReactServerView implements IReactServerView {
setAppError(SHUVI_ERROR.PAGE_NOT_FOUND);
}

if (error && error.fatal) {
return response('', { status: error.code, statusText: error.message });
/**
* @Note Please note that you should not use `error` directly, please
* use `app.error` instead.
* ❌ if (error && error.fatal)
* ✅ if (app.error && app.error.fatal)
*/
if (app.error && app.error.fatal) {
return response('', {
status: app.error.code,
statusText: app.error.message
});
}

if (redirected) {
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/loader/src/routes/fatal-error/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import React from 'react';
import { Loader } from '@shuvi/runtime';

async function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}

export const loader: Loader<any> = async ctx => {
/**
* Add a delay to verify the fatal error handling should work as
* expected.
*/
await sleep(100);
ctx.error('Not Found', 404, { fatal: true });
return null;
};
Expand Down

0 comments on commit 1b7d597

Please sign in to comment.