Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

feat: use netlify image loader for netlify templates (#2655)

Jaya Krishna Namburu6c2a26a4644a
diff --git a/fixtures/webstudio-remix-netlify-edge-functions/app/constants.mjs b/fixtures/webstudio-remix-netlify-edge-functions/app/constants.mjs
index 24fd396edf..9f14329fc8 100644
--- a/fixtures/webstudio-remix-netlify-edge-functions/app/constants.mjs
+++ b/fixtures/webstudio-remix-netlify-edge-functions/app/constants.mjs
@@ -8,6 +8,22 @@ export const imageBaseUrl = "/assets/";
/**
* @type {import("@webstudio-is/image").ImageLoader}
*/
-export const imageLoader = ({ src }) => {
- return imageBaseUrl + src;
+export const imageLoader = (props) => {
+ if (process.env.NODE_ENV !== "production") {
+ return imageBaseUrl + props.src;
+ }
+
+ if (props.format === "raw") {
+ return imageBaseUrl + props.src;
+ }
+
+ // https://docs.netlify.com/image-cdn/overview/
+ return (
+ "/.netlify/images?url=" +
+ encodeURIComponent(imageBaseUrl + props.src) +
+ "&w=" +
+ props.width +
+ "&q=" +
+ props.quality
+ );
};
diff --git a/fixtures/webstudio-remix-netlify-functions/app/constants.mjs b/fixtures/webstudio-remix-netlify-functions/app/constants.mjs
index 24fd396edf..9f14329fc8 100644
--- a/fixtures/webstudio-remix-netlify-functions/app/constants.mjs
+++ b/fixtures/webstudio-remix-netlify-functions/app/constants.mjs
@@ -8,6 +8,22 @@ export const imageBaseUrl = "/assets/";
/**
* @type {import("@webstudio-is/image").ImageLoader}
*/
-export const imageLoader = ({ src }) => {
- return imageBaseUrl + src;
+export const imageLoader = (props) => {
+ if (process.env.NODE_ENV !== "production") {
+ return imageBaseUrl + props.src;
+ }
+
+ if (props.format === "raw") {
+ return imageBaseUrl + props.src;
+ }
+
+ // https://docs.netlify.com/image-cdn/overview/
+ return (
+ "/.netlify/images?url=" +
+ encodeURIComponent(imageBaseUrl + props.src) +
+ "&w=" +
+ props.width +
+ "&q=" +
+ props.quality
+ );
};
diff --git a/packages/cli/templates/netlify-edge-functions/app/constants.mjs b/packages/cli/templates/netlify-edge-functions/app/constants.mjs
new file mode 100644
index 0000000000..9f14329fc8
--- /dev/null
+++ b/packages/cli/templates/netlify-edge-functions/app/constants.mjs
@@ -0,0 +1,29 @@
+/**
+ * We use mjs extension as constants in this file is shared with the build script
+ * and we use `node --eval` to extract the constants.
+ */
+export const assetBaseUrl = "/assets/";
+export const imageBaseUrl = "/assets/";
+
+/**
+ * @type {import("@webstudio-is/image").ImageLoader}
+ */
+export const imageLoader = (props) => {
+ if (process.env.NODE_ENV !== "production") {
+ return imageBaseUrl + props.src;
+ }
+
+ if (props.format === "raw") {
+ return imageBaseUrl + props.src;
+ }
+
+ // https://docs.netlify.com/image-cdn/overview/
+ return (
+ "/.netlify/images?url=" +
+ encodeURIComponent(imageBaseUrl + props.src) +
+ "&w=" +
+ props.width +
+ "&q=" +
+ props.quality
+ );
+};
diff --git a/packages/cli/templates/netlify-functions/app/constants.mjs b/packages/cli/templates/netlify-functions/app/constants.mjs
new file mode 100644
index 0000000000..9f14329fc8
--- /dev/null
+++ b/packages/cli/templates/netlify-functions/app/constants.mjs
@@ -0,0 +1,29 @@
+/**
+ * We use mjs extension as constants in this file is shared with the build script
+ * and we use `node --eval` to extract the constants.
+ */
+export const assetBaseUrl = "/assets/";
+export const imageBaseUrl = "/assets/";
+
+/**
+ * @type {import("@webstudio-is/image").ImageLoader}
+ */
+export const imageLoader = (props) => {
+ if (process.env.NODE_ENV !== "production") {
+ return imageBaseUrl + props.src;
+ }
+
+ if (props.format === "raw") {
+ return imageBaseUrl + props.src;
+ }
+
+ // https://docs.netlify.com/image-cdn/overview/
+ return (
+ "/.netlify/images?url=" +
+ encodeURIComponent(imageBaseUrl + props.src) +
+ "&w=" +
+ props.width +
+ "&q=" +
+ props.quality
+ );
+};