From ae174a9ed5bad1f695793676cb7ae43f56d6f479 Mon Sep 17 00:00:00 2001 From: Stefan Machhi Date: Sat, 30 May 2026 10:35:46 +0200 Subject: [PATCH 1/2] fix: remove dead else branch in csp insertDirective The else branch in `getCsp().insertDirective` called `.push` on `state.result.directives` after the if-condition had just established that value was nullish, which would have thrown a TypeError if reached. In practice `directives` is always initialized to an array, so the else was unreachable, but the shape of the code was misleading. Simplify to match the sibling `insertScriptResource` / `insertStyleResource` methods: guard on `state.result`, then update directives via pushDirective. --- .changeset/fix-csp-insert-directive.md | 5 +++++ packages/astro/src/core/fetch/fetch-state.ts | 4 +--- 2 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 .changeset/fix-csp-insert-directive.md diff --git a/.changeset/fix-csp-insert-directive.md b/.changeset/fix-csp-insert-directive.md new file mode 100644 index 000000000000..30e3c53cb9d3 --- /dev/null +++ b/.changeset/fix-csp-insert-directive.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Simplifies `context.csp.insertDirective` to remove an unreachable `else` branch that would have thrown a `TypeError` if its precondition ever held. The implementation now matches the pattern used by the sibling `insertScriptResource` / `insertStyleResource` methods. \ No newline at end of file diff --git a/packages/astro/src/core/fetch/fetch-state.ts b/packages/astro/src/core/fetch/fetch-state.ts index afbbfc5eb552..a4fe17ae837e 100644 --- a/packages/astro/src/core/fetch/fetch-state.ts +++ b/packages/astro/src/core/fetch/fetch-state.ts @@ -579,10 +579,8 @@ export class FetchState implements AstroFetchState { } return { insertDirective(payload) { - if (state?.result?.directives) { + if (state.result) { state.result.directives = pushDirective(state.result.directives, payload); - } else { - state?.result?.directives.push(payload); } }, insertScriptResource(resource) { From 05b63d0fb823676c89187367da04c522b6867d6f Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Tue, 2 Jun 2026 15:09:02 +0100 Subject: [PATCH 2/2] Delete .changeset/fix-csp-insert-directive.md --- .changeset/fix-csp-insert-directive.md | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 .changeset/fix-csp-insert-directive.md diff --git a/.changeset/fix-csp-insert-directive.md b/.changeset/fix-csp-insert-directive.md deleted file mode 100644 index 30e3c53cb9d3..000000000000 --- a/.changeset/fix-csp-insert-directive.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'astro': patch ---- - -Simplifies `context.csp.insertDirective` to remove an unreachable `else` branch that would have thrown a `TypeError` if its precondition ever held. The implementation now matches the pattern used by the sibling `insertScriptResource` / `insertStyleResource` methods. \ No newline at end of file