Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philhawksworth/1b7050c4a6af5b2b1ddb2a18d95b31a8 to your computer and use it in GitHub Desktop.
Save philhawksworth/1b7050c4a6af5b2b1ddb2a18d95b31a8 to your computer and use it in GitHub Desktop.
Example of an implementation using Netlify' On Demand Builder functions for DPR
require('dotenv').config();
const { builder } = require("@netlify/functions");
// Use the same page template our SSG uses dureing the build
const pageTemplate = require('../../src/site/_includes/layouts/idea.11ty.js');
const {
DATABASE_URL,
SUPABASE_SERVICE_API_KEY
} = process.env;
const handler = async event => {
// connect to database
const { createClient } = require('@supabase/supabase-js');
const supabase = createClient(DATABASE_URL, SUPABASE_SERVICE_API_KEY);
// Fetch the content at the specified path
const path = event.path.split("idea/")[1];
let { data: ideas, error } = await supabase
.from('ideas')
.select('*')
.eq('path', path);
console.log(`ODB render of ${path}`);
// render the data into the template
return {
statusCode: 200,
headers: {
"Content-Type": "text/html",
},
body: pageTemplate(ideas[0])
}
}
exports.handler = builder(handler);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment