Skip to content

fix: remove dead else branch in csp insertDirective#16921

Merged
ematipico merged 3 commits into
withastro:mainfrom
stefanmachhammer:fix/csp-insert-directive-dead-else
Jun 2, 2026
Merged

fix: remove dead else branch in csp insertDirective#16921
ematipico merged 3 commits into
withastro:mainfrom
stefanmachhammer:fix/csp-insert-directive-dead-else

Conversation

@stefanmachhammer
Copy link
Copy Markdown

Description

Simplifies the insertDirective method returned by context.csp (in packages/astro/src/core/fetch/fetch-state.ts).

The previous implementation had a misleading else branch:

insertDirective(payload) {
  if (state?.result?.directives) {
    state.result.directives = pushDirective(state.result.directives, payload);
  } else {
    state?.result?.directives.push(payload);
  }
},

Two problems:

  1. Dead code. state.result.directives is always initialized to an array (see fetch-state.ts:407directives: manifest.csp?.directives ? [...manifest.csp.directives] : []). Because empty arrays are truthy in JS, the if branch fires whenever state.result exists. The else is unreachable in practice.
  2. The else would crash if reached. Calling .push on the value that the if-condition just established is nullish would throw a TypeError. The optional-chaining ?. doesn't help — it only short-circuits up through state?.result?, not the final .push call.

This PR replaces both branches with the same single-statement pattern used by the sibling insertScriptResource / insertStyleResource methods directly below it.

Testing

Pure refactor of unreachable code; behavior under all currently-reachable inputs (i.e. whenever state.result exists, which is the only state the existing if-branch matches) is unchanged.

Docs

Not user-visible; no docs changes needed.

Changeset

Included.

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-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 30, 2026

⚠️ No Changeset found

Latest commit: 05b63d0

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions github-actions Bot added the pkg: astro Related to the core `astro` package (scope) label May 30, 2026
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 30, 2026

Merging this PR will not alter performance

✅ 18 untouched benchmarks


Comparing stefanmachhammer:fix/csp-insert-directive-dead-else (05b63d0) with main (79c6c46)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (e8fe0f5) during the generation of this report, so 79c6c46 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

Copy link
Copy Markdown
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

Good catch. I removed the changeset because it doesn't change any public API

@ematipico ematipico merged commit 7604e63 into withastro:main Jun 2, 2026
29 checks passed
ematipico added a commit that referenced this pull request Jun 2, 2026
* chore: move integration tests to unit tests (#16869)

* chore(deps): replace which-pm-runs with package-manager-detector (#16901)

* chore(deps): update dependency hono to v4.12.18 [security] (#16669)

* Fix false positive missing-content audit for hidden anchors (#16016)

* fix: remove dead else branch in csp insertDirective (#16921)

Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>

* chore: avoid formatter conflicts (#16897)

---------

Co-authored-by: ocavue <ocavue@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Felmon <felmonon@gmail.com>
Co-authored-by: Stefan Machhammer <stefan.machhi@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: astro Related to the core `astro` package (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants