Community Experiments for v3 Adapters #378
Replies: 5 comments 11 replies
-
This is awsome. I have elm-pages-3 running on express.js on fly.io: https://elm-pages-v3-express.fly.dev/ Source code is here: https://github.com/blaix/elm-pages-v3-express The adapter just copies the elm pages renderer and a basic express server into |
Beta Was this translation helpful? Give feedback.
-
Another adapter: @shahnhogan created a Fastify adatper with a starter repo. https://github.com/shahnhogan/elm-pages-starter-fastify. |
Beta Was this translation helpful? Give feedback.
-
I have an adapter using Koa/Node.js. The goal of this adapter is to be able to run it anywhere Node can run, in my case I use DigitalOcean for some projects and Google Cloud Run for others. I can install Node on the former and just run you will need to install both This code was heavily inspired by https://github.com/blaix/elm-pages-starter-express/tree/master/adapters/express. Thanks, @blaix. It would have taken me about 3x as long to figure it out without this repo. Under the main folder of the project, I have an adapter.mjs import * as fs from "fs";
export default async function run({
renderFunctionFilePath,
// routePatterns,
// apiRoutePatterns,
}) {
console.log("Running elm pages express adapter");
ensureDirSync("dist-server");
fs.copyFileSync(renderFunctionFilePath, "./dist-server/elm-pages.mjs");
fs.copyFileSync("./adapters/koa/server.mjs", "./dist-server/server.mjs");
fs.copyFileSync(
"./adapters/koa/middleware.mjs",
"./dist-server/middleware.mjs"
);
}
function ensureDirSync(dirpath) {
try {
fs.mkdirSync(dirpath, { recursive: true });
} catch (err) {
if (err.code !== "EEXIST") throw err;
}
} middlware.mjs import * as elmPages from "./elm-pages.mjs";
export default async (ctx, next) => {
try {
const { headers, statusCode, body, kind } = await elmPages.render(reqToElmPagesJson(ctx.request))
ctx.response.status = statusCode
for (const key in headers) {
ctx.set(key, headers[key]);
}
if (kind === "bytes") {
ctx.response.body = Buffer.from(body)
} else {
ctx.response.body = body
}
} catch (error) {
console.log("Encountered error serving request", ctx.request, error)
ctx.response.status = 500
// this can be styled, or taken out of a nice .html error file
ctx.response.body = "<body><h1>Error</h1><pre>Unexpected Error</pre></body>"
} finally {
next()
}
}
const reqToElmPagesJson = (req) => {
const url = `${req.protocol}://${req.host}${req.originalUrl}`;
return {
requestTime: Math.round(new Date().getTime()),
method: req.method,
headers: req.headers,
rawUrl: url,
body: req.body,
multiPartFormData: null,
};
}; server.mjs import Koa from 'koa';
import serve from 'koa-static';
import elmPagesMiddleware from './middleware.mjs';
const app = new Koa()
const port = 8000;
// Serve static files from 'dist' directory
app.use(serve('dist'));
// Use your custom middleware
app.use(elmPagesMiddleware);
// Start the server
app.listen(port, () => {
console.log(`Listening on port ${port}`);
}); I edited my import { defineConfig } from "vite";
import adapter from "./adapters/koa/adapter.mjs";
export default {
vite: defineConfig({}),
adapter,
headTagsTemplate(context) {
return `
<link rel="stylesheet" href="/style.css" />
<meta name="generator" content="elm-pages v${context.cliVersion}" />
`;
},
preloadTagForFile(file) {
// add preload directives for JS assets and font assets, etc., skip for CSS files
// this function will be called with each file that is procesed by Vite, including any files in your headTagsTemplate in your config
return !file.endsWith(".css");
},
}; To run this server, run |
Beta Was this translation helpful? Give feedback.
-
I've created a Hapi adapter based on the @blaix express adapter. |
Beta Was this translation helpful? Give feedback.
-
Here are the important bits of the AWS Lambda + APIGatewayV2 adapter we're using at my work: https://gist.github.com/adamdicarlo0/221e839050a3e8cef51f1849e7af71a9 I've included notes in a GitHub comment. I don't have a starter repo put together for it, at least not yet, but feel free to ask questions here! |
Beta Was this translation helpful? Give feedback.
-
I now have the v3 release candidate ready with a simplified adapter interface, which is documented here: https://elm-pages-v3.netlify.app/docs/adapters/.
There are a lot of possible frameworks and hosting platforms that we could build adapters for. Let's use this thread to share progress, experiments, and feedback. In particular:
A useful reference for building adapters is SvelteKit: https://kit.svelte.dev/docs/adapters. I got the idea of an adapter from SvelteKit initially, and they have some useful docs and implementations there.
Beta Was this translation helpful? Give feedback.
All reactions