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
5 changes: 5 additions & 0 deletions .changeset/clever-tigers-dig.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a case where `astro:env` secrets used in actions would not be available
5 changes: 5 additions & 0 deletions packages/astro/src/actions/noop-actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { SSRActions } from '../core/app/types.js';

export const NOOP_ACTIONS_MOD: SSRActions = {
server: {},
};
2 changes: 1 addition & 1 deletion packages/astro/src/core/app/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export type SSRManifest = {
key: Promise<CryptoKey>;
i18n: SSRManifestI18n | undefined;
middleware?: () => Promise<AstroMiddlewareInstance> | AstroMiddlewareInstance;
actions?: SSRActions;
actions?: () => Promise<SSRActions> | SSRActions;
checkOrigin: boolean;
sessionConfig?: ResolvedSessionConfig<any>;
cacheDir: string | URL;
Expand Down
5 changes: 3 additions & 2 deletions packages/astro/src/core/base-pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { NOOP_MIDDLEWARE_FN } from './middleware/noop-middleware.js';
import { sequence } from './middleware/sequence.js';
import { RouteCache } from './render/route-cache.js';
import { createDefaultRoutes } from './routing/default.js';
import { NOOP_ACTIONS_MOD } from '../actions/noop-actions.js';

/**
* The `Pipeline` represents the static parts of rendering that do not change between requests.
Expand Down Expand Up @@ -131,9 +132,9 @@ export abstract class Pipeline {
if (this.resolvedActions) {
return this.resolvedActions;
} else if (this.actions) {
return this.actions;
return await this.actions();
}
return { server: {} };
return NOOP_ACTIONS_MOD;
}

async getAction(path: string): Promise<ActionClient<unknown, ActionAccept, ZodType>> {
Expand Down
5 changes: 3 additions & 2 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import type {
StylesheetAsset,
} from './types.js';
import { getTimeStat, shouldAppendForwardSlash } from './util.js';
import { NOOP_ACTIONS_MOD } from '../../actions/noop-actions.js';

export async function generatePages(options: StaticBuildOptions, internals: BuildInternals) {
const generatePagesTimer = performance.now();
Expand All @@ -66,7 +67,7 @@ export async function generatePages(options: StaticBuildOptions, internals: Buil

const actions: SSRActions = internals.astroActionsEntryPoint
? await import(internals.astroActionsEntryPoint.toString()).then((mod) => mod)
: { server: {} };
: NOOP_ACTIONS_MOD;
manifest = createBuildManifest(
options.settings,
internals,
Expand Down Expand Up @@ -651,7 +652,7 @@ function createBuildManifest(
onRequest: middleware,
};
},
actions,
actions: () => actions,
checkOrigin:
(settings.config.security?.checkOrigin && settings.buildOutput === 'server') ?? false,
key,
Expand Down
3 changes: 1 addition & 2 deletions packages/astro/src/core/build/plugins/plugin-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ function generateSSRCode(adapter: AstroAdapter, middlewareId: string) {

const imports = [
`import { renderers } from '${RENDERERS_MODULE_ID}';`,
`import * as actions from '${ASTRO_ACTIONS_INTERNAL_MODULE_ID}';`,
`import * as serverEntrypointModule from '${ADAPTER_VIRTUAL_MODULE_ID}';`,
`import { manifest as defaultManifest } from '${SSR_MANIFEST_VIRTUAL_MODULE_ID}';`,
`import { serverIslandMap } from '${VIRTUAL_ISLAND_MAP_ID}';`,
Expand All @@ -180,7 +179,7 @@ function generateSSRCode(adapter: AstroAdapter, middlewareId: string) {
` pageMap,`,
` serverIslandMap,`,
` renderers,`,
` actions,`,
` actions: () => import("${ASTRO_ACTIONS_INTERNAL_MODULE_ID}"),`,
` middleware: ${edgeMiddleware ? 'undefined' : `() => import("${middlewareId}")`}`,
`});`,
`const _args = ${adapter.args ? JSON.stringify(adapter.args, null, 4) : 'undefined'};`,
Expand Down
7 changes: 7 additions & 0 deletions packages/integrations/cloudflare/test/astro-env.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,11 @@ describe('astro:env', () => {
const $ = cheerio.load(html);
assert.equal($('#secret').text().includes('123456789'), true);
});

it('action secret', async () => {
const res = await fetch('http://127.0.0.1:8788/test');
const html = await res.text();
const $ = cheerio.load(html);
assert.equal($('#secret').text().includes('123456789'), true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"astro": "workspace:*"
},
"devDependencies": {
"wrangler": "^3.112.0"
"wrangler": "^4.4.1"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { defineAction } from "astro:actions";
import { API_SECRET } from "astro:env/server";

export const server = {
getSecret: defineAction({
handler(_input, _context) {
return {
secret: API_SECRET,
};
},
}),
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineMiddleware } from 'astro/middleware'
import { API_SECRET } from 'astro:env/server'

const secret = API_SECRET

export const onRequest = defineMiddleware((_ctx, next) => {
console.log({ secret })
return next()
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
import { actions } from "astro:actions";
const { data } = await Astro.callAction(actions.getSecret, {});
---

<span id="secret">{data?.secret}</span>
86 changes: 43 additions & 43 deletions pnpm-lock.yaml

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