Skip to content

[v0.1.0] page:metadata hook never fires: locals.emdash missing collectPageMetadata in Astro middleware #362

Closed as not planned

Description

@davidrc

Description

The page:metadata plugin hook is documented and the dispatch infrastructure exists in HookPipeline, but the hook never fires when a public page is rendered. This blocks any plugin that needs to inject metadata, JSON-LD, og:tags, or other head contributions on public pages (SEO plugins, analytics, social embeds, schema markup).

Root cause: in dist/astro/middleware.mjs around line 1542, the middleware sets locals.emdash with all the API handlers but does not include collectPageMetadata or collectPageFragments. Since getPageRuntime() in src/page/index.ts requires both methods to be present, it always returns undefined, EmDashHead.astro skips the plugin contributions branch entirely, and only the base contributions (BlogPosting JSON-LD from core) are rendered.

Diagnosis: I patched the dist bundle with debug logs and confirmed:

  1. HookPipeline.registerPlugins IS called with my plugin
  2. The plugin's hooks object contains "page:metadata"
  3. registerPluginHook(plugin, "page:metadata") succeeds without warnings
  4. But hasHooks("page:metadata") and runPageMetadata() are never invoked when a page renders, because getPageRuntime() returns undefined

Suggested fix: add the two methods to locals.emdash in the Astro middleware:

locals.emdash = {                                                                                                                                               
  // ... existing handlers ...            
  collectPageMetadata: runtime.collectPageMetadata.bind(runtime),                                                                                               
  collectPageFragments: runtime.collectPageFragments.bind(runtime),                                                                                             
};

This single change should restore the entire page:metadata and page:fragments hook dispatch path.

Impact: any plugin that needs to inject head content on public pages is blocked. From a plugin author's perspective everything looks correct (the hook is documented, the manifest accepts it, registration succeeds without errors), which makes this a particularly silent failure.

I'm building an SEO plugin that depends on this hook. Happy to submit a PR with the fix if it would help.

Steps to reproduce

  1. Create a Standard plugin with page:metadata hook:

    // index.ts
    export function myPlugin(): PluginDescriptor {
      return {
        id: "my-plugin",
        version: "0.1.0",
        format: "standard",
        entrypoint: "@my-org/my-plugin/sandbox",
        capabilities: [],
      };
    }
    
    // sandbox-entry.ts
    import { definePlugin } from "emdash";
    
    export default definePlugin({
      hooks: {
        "page:metadata": async (event, ctx) => {
          ctx.log.info("page:metadata fired");
          return [
            { kind: "meta", name: "x-test", content: "hello", key: "x-test" },
          ];
        },
      },
    });
    
  2. Register in astro.config.mjs:

plugins: [myPlugin()],
  1. Start the dev server and load any public page (e.g., /posts/my-post).
  2. Expected: the log fires and appears in the rendered HTML.
  3. Actual: no log appears, no meta tag in the HTML. Only the core's base SEO contributions (BlogPosting JSON-LD) are rendered.

Environment

  • emdash version: 0.1.0
  • Node.js version: 22.22.2
  • Runtime: Node.js (@astrojs/node standalone, output: "server")
  • OS: macOS
  • Plugin format: Standard (config-based loading via plugins: [] in astro.config.mjs)
  • Theme: default npm create emdash@latest blog template (uses <EmDashHead page={pageCtx} /> in Base.astro)

Logs / error output

No errors. The hook silently does not fire. Confirmed via dist bundle patch:                                                                                                                                                        
[HOOK-DEBUG] registerPlugins called with 4 plugins                                                                                                              
[HOOK-DEBUG] plugin: my-plugin hooks: [ 'page:metadata' ]                                                                                                     
                                                                                                                                                                  
Plugin is registered, no skip warnings, but `hasHooks("page:metadata")` and `runPageMetadata()` never get called when pages render. Tracing through `dist/astro/middleware.mjs`:                                                                                                                                    
- `EmDashHead.astro` calls `getPageRuntime(Astro.locals)`                                                                                                       
- `getPageRuntime` returns `undefined` because `locals.emdash` does not have `collectPageMetadata`
- The `if (runtime)` branch is skipped, so `runtime.collectPageMetadata(page)` is never called                                                                  
- Only base contributions are rendered, plugin contributions are silently dropped

Activity

  1. added
    bug
    on Apr 7, 2026
  2. davidrc commented on Apr 8, 2026

    @davidrc
    Author

    Apologies — I missed #118 and #166 when filing this, which describe the same bug with more detail. #119 is an open PR fixing it by removing the anonymous fast-path in the middleware.

    A small extra data point from my investigation: I confirmed the broken path end-to-end by patching node_modules/emdash/dist/search-DG603UrT.mjs with debug logs in HookPipeline.registerPlugins, hasHooks, and runPageMetadata. The log trace showed:

    • registerPlugins is called and registers page:metadata for my plugin (no capability skip warning)
    • The hook is visible via Object.keys(plugin.hooks) at registration time
    • But hasHooks("page:metadata") and runPageMetadata() are never invoked when a page renders

    This confirms the diagnosis in #118: getPageRuntime() returns undefined because locals.emdash never gets collectPageMetadata/collectPageFragments bound, so EmDashHead.astro takes the fallback branch that skips plugin contributions entirely.

    Also worth noting: in my reproduction this happens for both anonymous and authenticated visitors (I was testing via /_emdash/api/auth/dev-bypass with a valid session cookie). If the fix in #119 only addresses the anonymous fast-path, please double-check that the authenticated path also populates collectPageMetadata in locals.emdash.

    Closing as duplicate of #118. +1 to merging #119.

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions