Created
May 4, 2021 10:02
-
-
Save philhawksworth/1b7050c4a6af5b2b1ddb2a18d95b31a8 to your computer and use it in GitHub Desktop.
Example of an implementation using Netlify' On Demand Builder functions for DPR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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