diff --git a/netlify.toml b/netlify.toml index fbb70bd..5f539f1 100644 --- a/netlify.toml +++ b/netlify.toml @@ -1,6 +1,7 @@ [build] publish = "public" - command = "echo No build command" + + functions = "netlify/functions/" node_bundler = "esbuild" [dev] diff --git a/netlify/functions/hello.js b/netlify/functions/hello.js index 0c8df43..3250ad9 100644 --- a/netlify/functions/hello.js +++ b/netlify/functions/hello.js @@ -5,11 +5,60 @@ // You can develop and deploy serverless functions right here as part // of your site. Netlify Functions will handle the rest for you. - +const axios = require('axios'); +const { JSDOM } = require('jsdom'); exports.handler = async event => { + + /* + const response = await axios.get('https://css-tricks.com/accessing-data-netlify-functions-react/'); const subject = event.queryStringParameters.name || 'World' return { statusCode: 200, - body: `Hello ${subject}!`, + body: `Hello ${subject}! ${response.data} `, } -} \ No newline at end of file +} +*/ + const url = "https://css-tricks.com/accessing-data-netlify-functions-react/"; + try { + const response = await axios.get(url); + const { document } = new JSDOM(response.data).window; + + const images = document.querySelectorAll('img'); + + images.forEach((img) => { + const dataUrl = getDataUrl(img.src); + img.src = dataUrl; + }); + + return { + statusCode: 200, + headers: { + 'Content-Type': 'text/html', + }, + body: document.documentElement.outerHTML, + }; + } catch (error) { + console.error('Error:', error.message); + return { + statusCode: 500, + body: 'An error occurred while rendering the page.', + }; + } +}; + + +function getDataUrl(url) { + return axios + .get(url, { + responseType: 'arraybuffer', + }) + .then((response) => { + const contentType = response.headers['content-type']; + const base64 = Buffer.from(response.data, 'binary').toString('base64'); + return `data:${contentType};base64,${base64}`; + }) + .catch((error) => { + console.error('Error fetching image:', error.message); + return url; // Return the original URL on error + }); +} diff --git a/package.json b/package.json index badedc7..28d9c26 100644 --- a/package.json +++ b/package.json @@ -3,5 +3,9 @@ "version": "1.0.0", "description": "A site to show a hello world example of using Netlify Functions", "author": "Netlify", - "license": "ISC" -} \ No newline at end of file + "license": "ISC", + "dependencies": { + "axios": "^1.4.0", + "jsdom": "^24.0.0" + } +}