Skip to content

page:metadata plugin hooks don't run for anonymous visitors on public pages #166

Closed

Description

@jdevalk

Problem

The middleware (src/astro/middleware.ts lines 200–228) skips runtime initialization for anonymous visitors on public pages. This means page:metadata plugin hooks never fire for the audience that matters most — search engine crawlers and regular visitors.

EmDashHead.astro checks for the runtime via getPageRuntime(Astro.locals) and falls back to base-only SEO contributions when it's absent. Any plugin using the page:metadata hook to contribute meta tags, Open Graph, canonical URLs, robots directives, or JSON-LD schema markup only works for logged-in editors.

Current behavior

For anonymous visitors on public pages:

middleware → early return (line 200-228) → locals.emdash not set
→ EmDashHead getPageRuntime() returns undefined → base contributions only

Plugin page:metadata hooks are never called. SEO plugins are effectively broken for the visitors that actually matter.

Expected behavior

page:metadata hooks should run for all visitors so plugins can contribute SEO tags that search engines see.

Why this is low-risk

The runtime is a singleton (runtimeInstance in middleware.ts). After the first request initializes it, getRuntime() is just a null-check. The per-request cost of calling runtime.collectPageMetadata(page) for anonymous visitors is negligible — it's just iterating registered hooks.

The early return at line 200 was likely an optimization to avoid session checks and full locals.emdash construction. But we don't need the full object — just the collectPageMetadata and collectPageFragments methods need to be available for EmDashHead.

Suggested fix

After the early-return block for anonymous visitors, still initialize the runtime and attach the page contribution methods to locals:

if (!isEmDashRoute && !isPublicRuntimeRoute && !hasEditCookie && !hasPreviewToken) {
    const sessionUser = await context.session?.get("user");
    if (!sessionUser && !playgroundDb) {
        // ... existing setup check ...

        // Initialize runtime for page:metadata hooks (cached singleton, negligible cost)
        try {
            const config = getConfig();
            if (config) {
                const runtime = await getRuntime(config);
                locals.emdash = {
                    collectPageMetadata: runtime.collectPageMetadata.bind(runtime),
                    collectPageFragments: runtime.collectPageFragments.bind(runtime),
                };
            }
        } catch {
            // Non-fatal — EmDashHead falls back to base contributions
        }

        const response = await next();
        setBaselineSecurityHeaders(response);
        return response;
    }
}

This keeps the optimization of not constructing the full locals.emdash admin object for anonymous visitors, while still enabling plugin hooks for SEO.

Use case

I'm building an SEO plugin (page:metadata hook) that generates:

  • Proper og:title (without site name suffix)
  • Meta robots with max-snippet:-1, max-image-preview:large, max-video-preview:-1
  • Canonical URLs with trailing slash normalization
  • Full JSON-LD schema graph (Person/Organization, WebSite, WebPage, Article)
  • Configurable Person/Organization settings via admin.settingsSchema

All of this works perfectly for logged-in editors but is invisible to search engines.

Activity

  1. added a commit that references this issue on Apr 3, 2026
    2ea1fc0
  2. blmyr commented on Apr 4, 2026

    @blmyr
    Contributor

    This is the same issue as #118, which I filed independently. I have an open PR fixing it: #119.

  3. ascorbic commented on Apr 20, 2026

    @ascorbic
    Member

    Fixed in #119 (merged Apr 11), which is the same fix as the linked #118. Plugin page:metadata and page:fragments hooks now fire for anonymous visitors on public pages, so SEO plugins work for the audience they need to. Closing as a duplicate of #118.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions