Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions packages/astro/src/core/middleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from '../../i18n/utils.js';
import type { MiddlewareHandler, Params, RewritePayload } from '../../types/public/common.js';
import type { APIContext, AstroSharedContextCsp } from '../../types/public/context.js';
import { ASTRO_VERSION, clientLocalsSymbol } from '../constants.js';
import { ASTRO_VERSION } from '../constants.js';
import { AstroCookies } from '../cookies/index.js';
import { AstroError, AstroErrorData } from '../errors/index.js';
import { getClientIpAddress } from '../routing/request.js';
Expand Down Expand Up @@ -43,7 +43,7 @@ export type CreateContext = {
/**
* Initial value of the locals
*/
locals: App.Locals;
locals?: App.Locals;
};

/**
Expand All @@ -54,7 +54,7 @@ function createContext({
params = {},
userDefinedLocales = [],
defaultLocale = '',
locals,
locals = {},
}: CreateContext): APIContext {
let preferredLocale: string | undefined = undefined;
let preferredLocaleList: string[] | undefined = undefined;
Expand Down Expand Up @@ -110,15 +110,10 @@ function createContext({
return clientIpAddress;
},
get locals() {
// TODO: deprecate this usage. This is used only by the edge middleware for now, so its usage should be basically none.
let _locals = locals ?? Reflect.get(request, clientLocalsSymbol);
if (locals === undefined) {
_locals = {};
}
if (typeof _locals !== 'object') {
if (typeof locals !== 'object') {
throw new AstroError(AstroErrorData.LocalsNotAnObject);
}
return _locals;
return locals;
},
set locals(_) {
throw new AstroError(AstroErrorData.LocalsReassigned);
Expand Down
10 changes: 1 addition & 9 deletions packages/astro/src/core/render-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import type { RouteData, SSRResult } from '../types/public/internal.js';
import type { SSRActions } from './app/types.js';
import {
ASTRO_VERSION,
clientAddressSymbol,
REROUTE_DIRECTIVE_HEADER,
REWRITE_DIRECTIVE_HEADER_KEY,
REWRITE_DIRECTIVE_HEADER_VALUE,
Expand Down Expand Up @@ -751,7 +750,7 @@ export class RenderContext {
}

getClientAddress() {
const { pipeline, request, routeData, clientAddress } = this;
const { pipeline, routeData, clientAddress } = this;

if (routeData.prerender) {
throw new AstroError({
Expand All @@ -764,13 +763,6 @@ export class RenderContext {
return clientAddress;
}

// TODO: Legacy, should not need to get here.
// Some adapters set this symbol so we can't remove support yet.
// Adapters should be updated to provide it via RenderOptions instead.
if (clientAddressSymbol in request) {
return Reflect.get(request, clientAddressSymbol) as string;
}

if (pipeline.adapterName) {
throw new AstroError({
...AstroErrorData.ClientAddressNotAvailable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ import { App } from 'astro/app';
}
}

Reflect.set(
request,
Symbol.for('astro.clientAddress'),
request.headers.get('cf-connecting-ip')
);

const locals = {
runtime: {
env: env,
Expand All @@ -44,7 +38,11 @@ import { App } from 'astro/app';
},
};

const response = await app.render(request, { routeData, locals });
const response = await app.render(request, {
routeData,
locals,
clientAddress: request.headers.get('cf-connecting-ip'),
});

if (app.setCookieHeaders) {
for (const setCookieHeader of app.setCookieHeaders(response)) {
Expand Down
9 changes: 7 additions & 2 deletions packages/astro/test/test-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,17 @@ export default function ({
return new Response(data);
}

return super.render(request, {
routeData,
locals,
addCookieHeader,
prerenderedErrorPageFetch,
${
provideAddress
? `request[Symbol.for('astro.clientAddress')] = clientAddress ?? '0.0.0.0';`
? `clientAddress: clientAddress ?? '0.0.0.0',`
: ''
}
return super.render(request, { routeData, locals, addCookieHeader, prerenderedErrorPageFetch });
});
}
}

Expand Down
3 changes: 1 addition & 2 deletions packages/integrations/cloudflare/src/utils/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ export async function handle(
}
}

Reflect.set(request, Symbol.for('astro.clientAddress'), request.headers.get('cf-connecting-ip'));

const locals: Runtime = {
runtime: {
env: env,
Expand All @@ -92,6 +90,7 @@ export async function handle(
prerenderedErrorPageFetch: async (url) => {
return env.ASSETS.fetch(url.replace(/\.html$/, ''));
},
clientAddress: request.headers.get('cf-connecting-ip') ?? undefined,
},
);

Expand Down
7 changes: 4 additions & 3 deletions packages/integrations/netlify/src/index.ts
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to match cloudflare

Original file line number Diff line number Diff line change
Expand Up @@ -698,10 +698,11 @@ export default function netlifyIntegration(
if (existingSessionModule) {
server.moduleGraph.invalidateModule(existingSessionModule);
}

const clientLocalsSymbol = Symbol.for('astro.locals');

server.middlewares.use((req, _res, next) => {
const locals = Symbol.for('astro.locals');
Reflect.set(req, locals, {
...Reflect.get(req, locals),
Reflect.set(req, clientLocalsSymbol, {
netlify: { context: getLocalDevNetlifyContext(req) },
});
next();
Expand Down
9 changes: 5 additions & 4 deletions packages/integrations/netlify/src/ssr-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ export interface Args {
middlewareSecret: string;
}

const clientAddressSymbol = Symbol.for('astro.clientAddress');

export const createExports = (manifest: SSRManifest, { middlewareSecret }: Args) => {
const app = new App(manifest);

Expand All @@ -30,7 +28,6 @@ export const createExports = (manifest: SSRManifest, { middlewareSecret }: Args)
});
}

Reflect.set(request, clientAddressSymbol, context.ip);
let locals: Record<string, unknown> = {};

const astroLocalsHeader = request.headers.get('x-astro-locals');
Expand All @@ -46,7 +43,11 @@ export const createExports = (manifest: SSRManifest, { middlewareSecret }: Args)

locals.netlify = { context };

const response = await app.render(request, { routeData, locals });
const response = await app.render(request, {
routeData,
locals,
clientAddress: context.ip
});

if (app.setCookieHeaders) {
for (const setCookieHeader of app.setCookieHeaders(response)) {
Expand Down
Loading