Frontity release: Server Extensibility

Starting from @frontity/core 1.6.0, you can now include server middleware in your packages! :tada:

It’s super simple: just add your middleware functions inside your package definition under the new server property.

export default {
  server: {
    myPackage: {
      myServerLogic: async ({ ctx, state, libraries, actions }) => {
        const someCustomHeader = await fetchSomething();
        ctx.set("X-Frontity-Some-Data", someCustomHeader);
        next();
      },
    },
  },
};

These functions receive the usual state/actions/libraries, but also the Koa’s ctx and next. Please refer to the Koa documentation to learn how to use ctx and next.

You can also import other Koa utilities and use them like in any other Koa app:

import { get } from "koa-route";

export default {
  server: {
    myPackage: {
      myRobotsTxt: get("/robots-one.txt", ({ ctx }) => {
        ctx.type = "text/plain";
        ctx.body = "User-agent: *\nAllow: /\n";
      }),
    },
  },
};

If you need to import server-only code, you need to divide the entry point of your package in client.js and server.js (instead of index.js).

You can type your server functions using the new Server type exported from frontity/types:

import { Server, Package } from "frontity/types";

interface MyPackage extends Package {
  // other state, actions...
  server: {
    myPackage: {
      myServerLogic: Server<MyPackage>;
    };
  };
}

export default MyPackage;

If you need to type your server functions in a call of a Koa middleware, you can manually cast the Server<MyPackage> type:

import { Server } from "frontity/types";
import { get } from "koa-route";
import MyPackage from "../types";

export default {
  server: {
    myPackage: {
      myRobotsTxt: get("/robots-one.txt", (({ ctx }) => {
        ctx.type = "text/plain";
        ctx.body = "User-agent: *\nAllow: /\n";
      }) as Server<MyPackage>),
    },
  },
};

If you want more details, you can read the Final Implementation Proposal and the Pull Request.

Huge kudos to @orballo for implementing this long-awaited feature!! :tada::tada:


@frontity/[email protected]

Minor Changes

@frontity/[email protected]

Minor Changes

2 Likes