Skip to content
This repository has been archived by the owner on Feb 10, 2025. It is now read-only.

Commit

Permalink
Merge branch 'main' into cloudflare-astro-v5
Browse files Browse the repository at this point in the history
  • Loading branch information
florian-lefebvre committed Dec 19, 2024
2 parents 60df0b7 + f487c91 commit 4565532
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package-lock.json
.pnpm-store
.idea/
**/fixtures/**/.astro
**/hosted/**/.astro

# ignore top-level vscode settings
/.vscode/settings.json
Expand Down
8 changes: 8 additions & 0 deletions packages/netlify/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# @astrojs/netlify

## 6.0.1

### Patch Changes

- [#481](https://github.com/withastro/adapters/pull/481) [`9d98b8a`](https://github.com/withastro/adapters/commit/9d98b8a19efdd5c7483cce70b732208093bf82b2) Thanks [@ascorbic](https://github.com/ascorbic)! - Fixes an error where edge middleware would incorrectly assign locals

- [#488](https://github.com/withastro/adapters/pull/488) [`f3739be`](https://github.com/withastro/adapters/commit/f3739bef812aa9659ff9bdd10ba9046ac716a3d5) Thanks [@ascorbic](https://github.com/ascorbic)! - Correctly pass Netlify context in edge middleware

## 6.0.0

### Major Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/netlify/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@astrojs/netlify",
"description": "Deploy your site to Netlify",
"version": "6.0.0",
"version": "6.0.1",
"type": "module",
"types": "./dist/index.d.ts",
"author": "withastro",
Expand Down
4 changes: 2 additions & 2 deletions packages/netlify/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,9 @@ export default function netlifyIntegration(
export default async (request, context) => {
const ctx = createContext({
request,
params: {}
params: {},
locals: { netlify: { context } }
});
ctx.locals = { netlify: { context } }
// https://docs.netlify.com/edge-functions/api/#return-a-rewrite
ctx.rewrite = (target) => {
if(target instanceof Request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { defineConfig } from 'astro/config';
// https://astro.build/config
export default defineConfig({
output: 'server',
adapter: netlify(),
adapter: netlify({
edgeMiddleware: true,
}),
image: {
remotePatterns: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import https from 'node:https';

export const onRequest = (context, next) => {
console.log(context.netlify);
context.locals.middleware = context?.locals?.netlify?.context?.geo?.country?.code ?? null;
context.locals.runtime = 'Deno' in globalThis ? 'Deno' : 'Node';
context.locals.title = 'Middleware';
context.locals.nodePrefixedImportExists = !!https;

return next();
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
const country = Astro.locals.middleware;
---

<h1>{country}</h1>
<h3>{country ? 'has context' : 'no context'}</h3>
<h2>{Astro.locals.runtime}</h2>
11 changes: 9 additions & 2 deletions packages/netlify/test/hosted/hosted.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@ describe('Hosted Netlify Tests', () => {
assert.equal(image.status, 200);
});

it('passes context from edge middleware', async () => {
const response = await fetch(`${NETLIFY_TEST_URL}/country`);
const body = await response.text();
assert.match(body, /has context/);
assert.match(body, /Deno/);
});

it('Server returns fresh content', async () => {
const responseOne = await fetch(`${NETLIFY_TEST_URL}/time`);
const responseOne = await fetch(`${NETLIFY_TEST_URL}/time`).then((res) => res.text());

const responseTwo = await fetch(`${NETLIFY_TEST_URL}/time`);
const responseTwo = await fetch(`${NETLIFY_TEST_URL}/time`).then((res) => res.text());

assert.notEqual(responseOne.body, responseTwo.body);
});
Expand Down

0 comments on commit 4565532

Please sign in to comment.