diff --git a/examples/docs/functions/render/index.js b/examples/docs/functions/render/index.js index e9d35f5d..a1577605 100644 --- a/examples/docs/functions/render/index.js +++ b/examples/docs/functions/render/index.js @@ -13,16 +13,33 @@ exports.handler = try { const renderResult = await renderer(compiledElmPath, event.path, event); - return { - body: renderResult.htmlString, - statusCode: 200, - }; + if (renderResult.kind === "json") { + return { + body: renderResult.contentJson, + headers: { + "Content-Type": "application/json", + "x-powered-by": "elm-pages", + }, + statusCode: 200, + }; + } else { + return { + body: renderResult.htmlString, + headers: { + "Content-Type": "text/html", + "x-powered-by": "elm-pages", + }, + statusCode: 200, + }; + } } catch (error) { return { - // body: JSON.stringify({ error }), body: `

Error

${error}
`, statusCode: 500, - headers: [{ "content-type": "text/html" }], + headers: { + "Content-Type": "text/html", + "x-powered-by": "elm-pages", + }, }; } }; diff --git a/generator/src/render.js b/generator/src/render.js index a7976f75..38d42e61 100755 --- a/generator/src/render.js +++ b/generator/src/render.js @@ -28,13 +28,23 @@ module.exports = * @param {string} compiledElmPath * @param {string} path * @param {import('aws-lambda').APIGatewayProxyEvent} request + * @returns {Promise<({ kind: 'json'; contentJson: string} | { kind: 'html'; htmlString: string })>} */ function runElmApp(compiledElmPath, path, request) { return new Promise((resolve, reject) => { + const isJson = path.match(/content\.json\/?$/); + const route = path.replace(/content\.json\/?$/, ""); + const mode /** @type { "dev" | "prod" } */ = "elm-to-html-beta"; const staticHttpCache = {}; + const modifiedRequest = { ...request, path: route }; const app = require(compiledElmPath).Elm.Main.init({ - flags: { secrets: process.env, mode, staticHttpCache, request }, + flags: { + secrets: process.env, + mode, + staticHttpCache, + request: modifiedRequest, + }, }); app.ports.toJsPort.subscribe((/** @type { FromElm } */ fromElm) => { @@ -42,8 +52,21 @@ function runElmApp(compiledElmPath, path, request) { console.log(fromElm.value); } else if (fromElm.tag === "PageProgress") { const args = fromElm.args[0]; - if ("/" + args.route === path) { - resolve(outputString(fromElm)); + if (isJson) { + if ("/" + args.route === route) { + let contentJson = {}; + contentJson["body"] = args.body; + + contentJson["staticData"] = args.contentJson; + resolve({ + kind: "json", + contentJson: JSON.stringify(contentJson), + }); + } + } else { + if ("/" + args.route === route) { + resolve(outputString(fromElm)); + } } } else if (fromElm.tag === "Errors") { foundErrors = true; @@ -108,6 +131,7 @@ async function outputString(/** @type { PageProgress } */ fromElm) { return { route: normalizedRoute, htmlString: wrapHtml(args, contentJsonString), + kind: "html", }; }