Unverified Commit 85e1ee5a authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Turbopack: Fix 500 handling in edge with pages router (#76155)

## What?

Ensures the pages/500.js is passed to the edge handler template when it
exists.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->
parent a89d55c0
......@@ -105,6 +105,7 @@ impl PagesProject {
app: _,
document: _,
error: _,
error_500: _,
} = &*pages_structure.await?;
let mut routes = FxIndexMap::default();
......@@ -711,7 +712,7 @@ impl PageEndpoint {
#[turbo_tasks::function]
fn source(&self) -> Vc<Box<dyn Source>> {
Vc::upcast(FileSource::new(self.page.project_path()))
Vc::upcast(FileSource::new(self.page.file_path()))
}
#[turbo_tasks::function]
......
......@@ -116,11 +116,13 @@ pub async fn create_page_ssr_entry_module(
INNER.into() => ssr_module,
};
let pages_structure_ref = pages_structure.await?;
if reference_type == ReferenceType::Entry(EntryReferenceSubType::Page) {
inner_assets.insert(
INNER_DOCUMENT.into(),
process_global_item(
pages_structure.document(),
*pages_structure_ref.document,
Value::new(reference_type.clone()),
ssr_module_context,
)
......@@ -130,7 +132,7 @@ pub async fn create_page_ssr_entry_module(
inner_assets.insert(
INNER_APP.into(),
process_global_item(
pages_structure.app(),
*pages_structure_ref.app,
Value::new(reference_type.clone()),
ssr_module_context,
)
......@@ -177,7 +179,7 @@ fn process_global_item(
reference_type: Value<ReferenceType>,
module_context: Vc<Box<dyn AssetContext>>,
) -> Vc<Box<dyn Module>> {
let source = Vc::upcast(FileSource::new(item.project_path()));
let source = Vc::upcast(FileSource::new(item.file_path()));
module_context.process(source, reference_type).module()
}
......@@ -197,6 +199,7 @@ async fn wrap_edge_page(
const INNER_DOCUMENT: &str = "INNER_DOCUMENT";
const INNER_APP: &str = "INNER_APP";
const INNER_ERROR: &str = "INNER_ERROR";
const INNER_ERROR_500: &str = "INNER_500";
let next_config_val = &*next_config.await?;
......@@ -235,30 +238,41 @@ async fn wrap_edge_page(
fxindexmap! {
// TODO
"incrementalCacheHandler" => None,
"userland500Page" => None,
"userland500Page" => pages_structure.await?.error_500.map(|_| INNER_ERROR_500.into()),
},
)
.await?;
let inner_assets = fxindexmap! {
let pages_structure_ref = pages_structure.await?;
let mut inner_assets = fxindexmap! {
INNER.into() => entry,
INNER_DOCUMENT.into() => process_global_item(
pages_structure.document(),
*pages_structure_ref.document,
reference_type.clone(),
asset_context,
).to_resolved().await?,
INNER_APP.into() => process_global_item(
pages_structure.app(),
*pages_structure_ref.app,
reference_type.clone(),
asset_context,
).to_resolved().await?,
INNER_ERROR.into() => process_global_item(
pages_structure.error(),
*pages_structure_ref.error,
reference_type.clone(),
asset_context,
).to_resolved().await?,
};
if let Some(error_500) = pages_structure_ref.error_500 {
inner_assets.insert(
INNER_ERROR_500.into(),
process_global_item(*error_500, reference_type.clone(), asset_context)
.to_resolved()
.await?,
);
}
let wrapped = asset_context
.process(
Vc::upcast(source),
......
use anyhow::Result;
use tracing::Instrument;
use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, TryJoinIterExt, ValueToString, Vc};
use turbo_tasks::{OptionVcExt, ResolvedVc, TryJoinIterExt, ValueToString, Vc};
use turbo_tasks_fs::{
DirectoryContent, DirectoryEntry, FileSystemEntryType, FileSystemPath, FileSystemPathOption,
};
......@@ -45,17 +45,22 @@ impl PagesStructureItem {
}
#[turbo_tasks::function]
pub async fn project_path(&self) -> Result<Vc<FileSystemPath>> {
pub async fn file_path(&self) -> Result<Vc<FileSystemPath>> {
// Check if the file path + extension exists in the filesystem, if so use that. If not fall
// back to the base path.
for ext in self.extensions.await?.into_iter() {
let project_path = self.base_path.append(format!(".{ext}").into());
let ty = *project_path.get_type().await?;
let file_path: Vc<FileSystemPath> = self.base_path.append(format!(".{ext}").into());
let ty = *file_path.get_type().await?;
if matches!(ty, FileSystemEntryType::File | FileSystemEntryType::Symlink) {
return Ok(project_path);
return Ok(file_path);
}
}
if let Some(fallback_path) = self.fallback_path {
Ok(*fallback_path)
} else {
// If the file path that was passed in already has an extension, for example
// `pages/index.js` it won't match the extensions list above because it already had an
// extension and for example `.js.js` obviously won't match
Ok(*self.base_path)
}
}
......@@ -68,28 +73,11 @@ pub struct PagesStructure {
pub app: ResolvedVc<PagesStructureItem>,
pub document: ResolvedVc<PagesStructureItem>,
pub error: ResolvedVc<PagesStructureItem>,
pub error_500: Option<ResolvedVc<PagesStructureItem>>,
pub api: Option<ResolvedVc<PagesDirectoryStructure>>,
pub pages: Option<ResolvedVc<PagesDirectoryStructure>>,
}
#[turbo_tasks::value_impl]
impl PagesStructure {
#[turbo_tasks::function]
pub fn app(&self) -> Vc<PagesStructureItem> {
*self.app
}
#[turbo_tasks::function]
pub fn document(&self) -> Vc<PagesStructureItem> {
*self.document
}
#[turbo_tasks::function]
pub fn error(&self) -> Vc<PagesStructureItem> {
*self.error
}
}
#[turbo_tasks::value]
pub struct PagesDirectoryStructure {
pub project_path: ResolvedVc<FileSystemPath>,
......@@ -157,6 +145,7 @@ async fn get_pages_structure_for_root_directory(
let page_extensions_raw = &*page_extensions.await?;
let mut api_directory = None;
let mut error_500_item = None;
let project_path = project_path.await?;
let pages_directory = if let Some(project_path) = &*project_path {
......@@ -179,6 +168,23 @@ async fn get_pages_structure_for_root_directory(
let base_path = project_path.join(basename.into());
match basename {
"_app" | "_document" | "_error" => {}
"500" => {
let item_next_router_path =
next_router_path_for_basename(next_router_path, basename);
let item_original_path = next_router_path.join(basename.into());
let item = PagesStructureItem::new(
base_path,
page_extensions,
None,
item_next_router_path,
item_original_path,
);
error_500_item = Some(item);
items.push((basename, item));
}
basename => {
let item_next_router_path =
next_router_path_for_basename(next_router_path, basename);
......@@ -294,6 +300,7 @@ async fn get_pages_structure_for_root_directory(
app: app_item.to_resolved().await?,
document: document_item.to_resolved().await?,
error: error_item.to_resolved().await?,
error_500: error_500_item.to_resolved().await?,
api: api_directory,
pages: pages_directory,
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment