Skip to content

Support scoped server loaders for nested Pages layouts #882

Open

Description

@karthickeyan-dev

Our admin layout displays an unread-report count shared by several nested pages. Middleware reads it before each request and puts it in shared. When a page action marks a report as read, the action’s page response still contains the pre-action count unless the handler refreshes it explicitly. Each action that changes the count has to know about the sidebar, and the admin-only field appears in the application-wide shared type.

What I checked

In a four-file React Pages fixture with void@1.11.0, I ran the installed page scanner with these files:

pages/admin/layout.tsx
pages/admin/layout.server.ts   // exports loader
pages/admin/index.tsx
pages/admin/index.server.ts    // exports loader

The page entry recorded serverPath: "admin/index.server.ts" and methods: ["loader"]. The layout entry recorded its directory and component path only. The Pages actions guide also documents that middleware does not run a second time when the action rerenders the page.

Request

Let a layout supply a server loader, for example through pages/admin/layout.server.ts, and expose the inferred result to that layout. Run it for requests that render a descendant page, including a page refresh after an action, so its data reflects the completed mutation. Pages outside that layout should incur no read. For the sidebar case this would let the layout own its count and let each page action own its mutation.

The existing workaround is to recompute the shared field in the action before returning, or redirect to make a fresh request.

Activity

  1. hyfdev commented on Nov 29, 2026

    @hyfdev
    Contributor

    You can keep the count fresh today by computing it in middleware on each page loader under admin/, instead of in application middleware.

    The loader runs again when an action rerenders the page, so it reads the count after the mutation:

    // pages/admin/load-sidebar.ts
    import { defineMiddleware } from 'void';
    
    export const loadSidebar = defineMiddleware(async (c, next) => {
      const shared = { ...c.get('shared'), sidebarUnread: await countUnread(c) };
      c.set('shared', shared);
      await next();
    });
    // pages/admin/index.server.ts
    import { defineHandler } from 'void';
    import { loadSidebar } from './load-sidebar.ts';
    
    export const loader = defineHandler(loadSidebar, async (c) => ({ reports: await listReports(c) }));
    
    export const actions = {
      markRead: defineHandler(async (c) => {
        await markReportRead(c);
      }),
    };
    // pages/admin/layout.tsx
    import { useShared } from '@void/react';
    
    // inside the layout component
    const { sidebarUnread } = useShared<{ sidebarUnread: number }>();

    The action doesn't need to know about the sidebar.

    Pages outside admin/ never compute the count, and the field stays out of the app-wide shared type.

    The cost is that every page under the layout needs a loader with loadSidebar, and you write the useShared type by hand.

  2. karthickeyan-dev commented on Nov 29, 2026

    @karthickeyan-dev
    ContributorAuthor

    Thanks, that works. The catch is that every page under admin/ has to compose loadSidebar into its loader, and one that forgets it shows a stale badge with no error. The count also still lands in the site-wide shared type. A layout.server.ts loader would put it in one place no page can miss, so I'll keep our current workaround until then.

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

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions