From cbcfd434101db269c6b14b187b55bf7899f0b805 Mon Sep 17 00:00:00 2001 From: "DESKTOP-PVAS0TJ\\lucas" Date: Fri, 12 May 2023 22:34:36 -0400 Subject: [PATCH] Cloudflare compatibility(search,redirect,404) -Wrangler.toml to allow module syntax in page functions -404 changes to allow cloudflare auto 404 recognition -Functions/search.js for search functionality -_redirects for user specified redirection -.node-version for specifiying the node version for cloudflare builds -.elventy.js changes to passthrough _redirects --- .eleventy.js | 2 +- .node-version | 1 + functions/api/search.js | 22 ++++++++++++++++++++++ src/site/404.njk | 11 ++++++++++- src/site/cloudflare/_redirects | 0 wrangler.toml | 3 +++ 6 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 .node-version create mode 100644 functions/api/search.js create mode 100644 src/site/cloudflare/_redirects create mode 100644 wrangler.toml diff --git a/.eleventy.js b/.eleventy.js index 065fe0094..2ed59bdbf 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -404,7 +404,7 @@ module.exports = function (eleventyConfig) { } return content; }); - + eleventyConfig.addPassthroughCopy({ "src/site/cloudflare/": "/" }); eleventyConfig.addPassthroughCopy("src/site/img"); eleventyConfig.addPassthroughCopy("src/site/scripts"); eleventyConfig.addPassthroughCopy("src/site/styles/_theme.*.css"); diff --git a/.node-version b/.node-version new file mode 100644 index 000000000..f7ae049ea --- /dev/null +++ b/.node-version @@ -0,0 +1 @@ +20.1.0 \ No newline at end of file diff --git a/functions/api/search.js b/functions/api/search.js new file mode 100644 index 000000000..299c64ca1 --- /dev/null +++ b/functions/api/search.js @@ -0,0 +1,22 @@ +//cloudflare(page-function) search function +//requires wrangler.toml at root directory for module syntax +//requires .node-version >16 at root directory for cloudflare(page-function) +const searchHandler = require('../../netlify/functions/search/search.js').handler; +export async function onRequest({ request, next}) { + const response = await next(); + + let requestQueryParams = Object.fromEntries(new URL(request.url).searchParams); + + let event = {queryStringParameters: requestQueryParams}; + + let searchResponse = await searchHandler(event); + + const newResponse = new Response(searchResponse.body, { headers: { + 'content-type': 'application/json; charset=utf-8', + response, + } + }); + + return newResponse; +} + diff --git a/src/site/404.njk b/src/site/404.njk index 467b7fd1f..a9a6c07c5 100644 --- a/src/site/404.njk +++ b/src/site/404.njk @@ -1,4 +1,13 @@ - +--- +pagination: + size: 1 + data: mypages + alias: myurl +mypages: + - 404.html + - 404/index.html +permalink: "{{ myurl }}" +--- diff --git a/src/site/cloudflare/_redirects b/src/site/cloudflare/_redirects new file mode 100644 index 000000000..e69de29bb diff --git a/wrangler.toml b/wrangler.toml new file mode 100644 index 000000000..6708c1b1c --- /dev/null +++ b/wrangler.toml @@ -0,0 +1,3 @@ +[build.upload] +format = "modules" +main = "./worker.mjs" \ No newline at end of file