-
Notifications
You must be signed in to change notification settings - Fork 67
initial support for next/image on netlify #138
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const { copySync } = require("fs-extra"); | ||
const { join } = require("path"); | ||
const { NEXT_IMAGE_FUNCTION_NAME, TEMPLATES_DIR } = require("../config"); | ||
|
||
// Move our next/image function into the correct functions directory | ||
const setupImageFunction = (functionsPath) => { | ||
const functionName = `${NEXT_IMAGE_FUNCTION_NAME}.js`; | ||
const functionDirectory = join(functionsPath, functionName); | ||
|
||
copySync(join(TEMPLATES_DIR, "imageFunction.js"), functionDirectory, { | ||
overwrite: false, | ||
errorOnExist: true, | ||
}); | ||
}; | ||
|
||
module.exports = setupImageFunction; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const jimp = require("jimp"); | ||
|
||
// Function used to mimic next/image and sharp | ||
exports.handler = async (event) => { | ||
const { url, w = 500, q = 75 } = event.queryStringParameters; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure about these defaults but i think it's fine since next_image should almost always provide There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we probably want some reasonable default just in case so we don't end up trying to process 6MB images at full res or whatever, but I think you're right that it should always be set |
||
const width = parseInt(w); | ||
const quality = parseInt(q); | ||
|
||
const imageUrl = url.startsWith("/") | ||
? `${process.env.URL || "http://localhost:8888"}${url}` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure about this ||, but i think it's fine since ntl dev uses 8888 as its default port right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is probably okay in the short term, but the port is configurable. quick fix is the create a |
||
: url; | ||
const image = await jimp.read(imageUrl); | ||
|
||
image.resize(width, jimp.AUTO).quality(quality); | ||
|
||
const imageBuffer = await image.getBufferAsync(image.getMIME()); | ||
|
||
return { | ||
statusCode: 200, | ||
headers: { | ||
"Content-Type": image.getMIME(), | ||
}, | ||
body: imageBuffer.toString("base64"), | ||
isBase64Encoded: true, | ||
}; | ||
}; |
Uh oh!
There was an error while loading. Please reload this page.