Skip to content

Commit

Permalink
fix: non-index pages with trailing slash rewriting to /index for rs…
Browse files Browse the repository at this point in the history
…c requests (#291)

* fix: non-index pages with trailing slash rewriting to `/index`

* chore: changeset
  • Loading branch information
james-elicx authored Jun 5, 2023
1 parent ff90dde commit 7deb9d8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/smooth-pumpkins-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@cloudflare/next-on-pages': patch
---

Fix non-index pages with trailing slash rewriting to /index for rsc requests.
2 changes: 1 addition & 1 deletion templates/_worker.js/routes-matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ export class RoutesMatcher {
// NOTE: Special handling for `/index` RSC routes. Sometimes the Vercel build output config
// has a record to rewrite `^/` to `/index.rsc`, however, this will hit requests to pages
// that aren't `/`. In this case, we should check that the previous path is `/`.
if (/\/index\.rsc$/i.test(this.path) && !/\/(?:index)?$/i.test(prevPath)) {
if (/\/index\.rsc$/i.test(this.path) && !/^\/(?:index)?$/i.test(prevPath)) {
this.path = prevPath;
}

Expand Down
16 changes: 16 additions & 0 deletions tests/templates/requestTestData/basicEdgeAppDir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ export const testSet: TestSet = {
api: { 'hello.func': createValidFuncDir('/api/hello') },
'index.func': createValidFuncDir('/index'),
'index.rsc.func': createValidFuncDir('/index.rsc'),
'alternative.func': createValidFuncDir('/alternative'),
'alternative.rsc.func': createValidFuncDir('/alternative.rsc'),
},
static: {
_next: {
Expand Down Expand Up @@ -235,5 +237,19 @@ export const testSet: TestSet = {
},
},
},
{
name: "non-index page with trailing slash and rsc header doesn't redirect to /index",
paths: ['/alternative/'],
headers: { rsc: '1' },
expected: {
status: 200,
data: JSON.stringify({ file: '/alternative', params: [] }),
headers: {
'content-type': 'text/plain;charset=UTF-8',
'x-matched-path': '/alternative.rsc',
vary: 'RSC, Next-Router-State-Tree, Next-Router-Prefetch',
},
},
},
],
};

0 comments on commit 7deb9d8

Please sign in to comment.