Skip to content

Commit

Permalink
make sure headers set in middlewares are accessibly by layouts and pa…
Browse files Browse the repository at this point in the history
…ges (#811)

* make sure headers set in middlewares are accessibly by layouts and pages

* update tsconfig to include vitest config file
  • Loading branch information
dario-piotrowicz authored Jun 27, 2024
1 parent fe94720 commit 9dd9fe4
Show file tree
Hide file tree
Showing 19 changed files with 5,266 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-falcons-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@cloudflare/next-on-pages': patch
---

make sure headers set in middlewares are accessibly by layouts and pages
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/next-on-pages/templates/_worker.js/routes-matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ export class RoutesMatcher {
this.status = resp.status;
}

// copy to the request object the headers that have been set by the middleware
applyHeaders(this.reqCtx.request.headers, resp.headers);

applyHeaders(this.headers.normal, resp.headers);
this.headers.middlewareLocation = resp.headers.get('location');
}
Expand Down
27 changes: 27 additions & 0 deletions pages-e2e/features/issue797/issue.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { getAssertVisible } from '@features-utils/getAssertVisible';
import { describe, expect, test } from 'vitest';

describe('issue-797', () => {
test('should pass headers set in the middleware along to layouts and pages', async () => {
const page = await BROWSER.newPage();
const assertVisible = getAssertVisible(page);
const pageUrl = `${DEPLOYMENT_URL}/`;
await page.goto(pageUrl);
const html = await assertVisible('html');

const layoutAttrHeaderFromMiddleware = await html.getAttribute(
'layout-attr-header-from-middleware',
);

expect(layoutAttrHeaderFromMiddleware).toEqual(
'this is a header set by the middleware!',
);

const gottenReqHeaderP = await assertVisible(
'p[data-test-id="header-from-middleware"]',
);
expect(await gottenReqHeaderP.innerText()).toEqual(
'header from middleware: this is a header set by the middleware!',
);
});
});
3 changes: 3 additions & 0 deletions pages-e2e/features/issue797/main.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"setup": "node --loader tsm setup.ts"
}
1 change: 1 addition & 0 deletions pages-e2e/features/issue797/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// no setup required
38 changes: 38 additions & 0 deletions pages-e2e/fixtures/issue797app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js
.yarn/install-state.gz

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts

.wrangler
4 changes: 4 additions & 0 deletions pages-e2e/fixtures/issue797app/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Generated by Wrangler
// by running `wrangler types --env-interface CloudflareEnv env.d.ts`

interface CloudflareEnv {}
12 changes: 12 additions & 0 deletions pages-e2e/fixtures/issue797app/main.fixture
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"features": [
"issue797"
],
"buildConfig": {
"buildCommand": "npx --force ../../../packages/next-on-pages",
"buildOutputDirectory": ".vercel/output/static"
},
"deploymentConfig": {
"compatibilityFlags": ["nodejs_compat"]
}
}
13 changes: 13 additions & 0 deletions pages-e2e/fixtures/issue797app/next.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { setupDevPlatform } from '@cloudflare/next-on-pages/next-dev';

// Here we use the @cloudflare/next-on-pages next-dev module to allow us to use bindings during local development
// (when running the application with `next dev`), for more information see:
// https://github.com/cloudflare/next-on-pages/blob/5712c57ea7/internal-packages/next-dev/README.md
if (process.env.NODE_ENV === 'development') {
await setupDevPlatform();
}

/** @type {import('next').NextConfig} */
const nextConfig = {};

export default nextConfig;
Loading

0 comments on commit 9dd9fe4

Please sign in to comment.