Skip to content

Release

Velocity 0.2: Content Security Policy On by Default

Velocity 0.2 beta turns on a strict Content Security Policy, moves environment variables to astro:env, drops astro-seo for native meta tags and adds an upgrade manifest.

Release No. 04

Most starter kits leave security headers for later. Later usually means never, or it means the week before launch, when adding a Content Security Policy breaks analytics, fonts and inline scripts all at once.

Environment variables have the same problem. A typo in import.meta.env.PUBLIC_GTM_ID doesn’t fail the build. It just quietly ships undefined.

Velocity 0.2 beta fixes both at the starting line. CSP is on from the start, environment variables are typed and validated, and the SEO tags are plain HTML you can read. It also moves Velocity to Astro 6.0.0-beta.9.

Content Security Policy, on by default

Astro 6 can generate a Content Security Policy for you. It hashes the scripts and styles it bundles and adds the policy to each page, so you don’t maintain a list of hashes by hand. Velocity 0.2 turns it on in astro.config.mjs.

A bare csp: true blocks anything that isn’t served from your own origin, and Velocity’s analytics component loads Google Tag Manager and Google Analytics. So 0.2 ships an explicit allowlist instead:

security: {
  checkOrigin: true,
  csp: {
    scriptDirective: {
      resources: [
        "'self'",
        'https://www.googletagmanager.com',
        'https://www.google-analytics.com',
      ],
    },
    styleDirective: {
      resources: ["'self'", 'https://fonts.googleapis.com'],
    },
    directives: [
      "font-src 'self' https://fonts.gstatic.com",
      "connect-src 'self' https://www.google-analytics.com https://www.googletagmanager.com",
      "img-src 'self' data: https://www.googletagmanager.com",
    ],
  },
},

Every external host is listed in one place. If you add a new third-party script, you add its domain here, and you can see exactly what your site is allowed to load. The Astro configuration reference covers the full set of options.

Type-safe environment variables with astro:env

Velocity 0.2 defines every environment variable it uses in an env.schema block, using Astro’s astro:env:

VariableContextAccess
SITE_URLserverpublic
PUBLIC_GA_MEASUREMENT_IDclientpublic
PUBLIC_GTM_IDclientpublic
CONTACT_FORM_ENDPOINTserversecret
NEWSLETTER_API_KEYserversecret
GOOGLE_SITE_VERIFICATIONserverpublic
BING_SITE_VERIFICATIONserverpublic

All of them are optional, so you only set the ones you use. Code imports them by name instead of reading import.meta.env:

import { PUBLIC_GA_MEASUREMENT_ID, PUBLIC_GTM_ID } from 'astro:env/client';

Astro generates the types, so the hand-written ImportMetaEnv interface in env.d.ts is gone. Secrets such as NEWSLETTER_API_KEY are marked secret and can only be imported on the server, which keeps them out of client bundles.

Native SEO meta tags

Velocity used the astro-seo package to render meta tags. In 0.2 the SEO component writes them as native HTML instead. The props are the same, so no page needed to change, and the package is no longer a dependency.

The component renders the title, description, canonical URL, robots, Open Graph tags with article metadata, the Twitter card and search engine verification tags. Writing them directly made it easy to fix three problems in the same release:

  • Twitter cards: twitter:title, twitter:description and twitter:image were missing, so previews on X didn’t render correctly. They’re now included.
  • Open Graph locale: a locale such as en is now mapped to the en_US format Open Graph expects.
  • Image metadata: width and height are only emitted for the auto-generated 1200 by 630 share image, and og:image:type is taken from the file extension for custom images.

We also removed a second <link rel="canonical"> from BaseLayout. The SEO component already rendered one, so every page had two.

An upgrade manifest

Velocity projects are scaffolded by the create-velocity-astro CLI. 0.2 adds velocity-manifest.json, which tells the CLI how to upgrade an existing project:

  • Safe files Velocity can replace, such as src/components/ui/, src/layouts/ and src/styles/tokens/.
  • Protected files that belong to you, such as src/config/, src/pages/, src/content/ and .env.
  • Dependencies to update or remove. For 0.2 that means Astro 6.0.0-beta.9, removing zod and removing astro-seo.
  • Migrations for the changes the CLI can’t make for you, each with a short description.

The manifest requires CLI version 1.6.0 or later.

Zod 4 and other fixes

Astro 6 bundles Zod 4, so Velocity now imports z from astro/zod and no longer depends on the standalone zod package. That meant a few code changes in the contact and newsletter API routes:

  • z.string().email() becomes z.email().
  • ZodError.errors becomes ZodError.issues.

Both changes are listed as migrations in the manifest. Also in this release:

  • @astrojs/mdx and @astrojs/react move to their v5 betas.
  • The experimental.contentIntellisense flag is removed, since Astro has stabilized it.
  • The legacy --ext flag is gone from the ESLint scripts.
  • package-lock.json is removed. Velocity’s own repo uses pnpm only.

Get it

Start a new project:

pnpm create velocity-astro my-project

If you started on 0.1, the manifest lists what changed, including the config blocks to add for astro:env and CSP. The full history is on GitHub. If you missed the first release, here’s where Velocity started.

All posts
Share:

Related posts