Skip to content

Commit

Permalink
Add edge function
Browse files Browse the repository at this point in the history
  • Loading branch information
igneosaur committed Jul 27, 2023
1 parent f4cd82e commit 1b571f7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ public
src/gatsby-types.d.ts
.env.*
!.env.example
.netlify
29 changes: 29 additions & 0 deletions netlify/edge-functions/experiment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Context } from "https://edge.netlify.com";
import { HTMLRewriter } from "https://raw.githubusercontent.com/worker-tools/html-rewriter/master/index.ts";

export default async (request: Request, context: Context) => {
const next = await context.next();
let config: Record<string, string> = {};
let buffer = "";
return new HTMLRewriter()
.on("#edge-config", {
text(scriptText) {
buffer += scriptText.text;
if (scriptText.lastInTextNode) {
config = JSON.parse(buffer);
}
},
})
.on("body", {
element(bodyEl) {
bodyEl.setAttribute(
"class",
["experiment-grey-bkg-bucket-a", config.bodyClass].join(" ")
);
console.log({ config });
},
})
.transform(next);
};

export const config = { path: "/" };
10 changes: 9 additions & 1 deletion src/templates/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ const IndexPage = ({ data }: PageProps<Queries.PageQuery>) => {

export default IndexPage;

export const Head: HeadFC = () => <title>Home Page</title>;
export const Head: HeadFC<Queries.PageQuery> = ({ data }) => (
<>
<title>Home Page</title>
{/* @ts-ignore */}
<script id="edge-config" language="json">
{JSON.stringify({ bodyClass: "test-from-edge", data })}
</script>
</>
);

export const query = graphql`
query Page($slug: String!) {
Expand Down

0 comments on commit 1b571f7

Please sign in to comment.