-
-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
svgo
and SSR
#35
Comments
SVGO and SSRTo add more context to the issue, the error that is thrown when building is this:
Many of the SSR adapters enable
Fetching SVGs and SSRAnother issue with SSR is that the default behaviour of I'm not sure how to fix all of this. It seems like the perfect solution would be to make the SVG fetching and optimisation happen at the build step instead of at runtime, but looking at the Astro Integration API docs I'm not sure we can hook into that and change the source code during build? @natemoo-re |
I have the exact same issue. |
Having the same issue when trying to use with Cloudflare Pages |
Resolution / update badly needed. Cannot deploy to CF pages using SSR :-( |
After doing some research, I have found that svgo actually includes a browser version as well as a node version. I have feebly tried to fork and create a PR but I cannot get it working properly on my dev server. Node complains about unexpected exports, even though a diff between the versions shows only some work in the optimize function... Sigh. Anyway, one can import My sad attempt is here, and for whatever reason I can't get this to run on my dev server. In any case, it results in the same issue at CF where it can't bundle the node version. Maybe distributing two separate packages?? https://github.com/simpleauthority/astro-icon/tree/feat/ssr-support |
hello @simpleauthority i opened an issue #52 and i suggested to use a javascript native approach and not depend on nodejs , i can help with a fix , i just need some hints or approach, i will search for alternatives later |
Yeah I am not sure if there is anything that is plain JS or not. I'll have to keep looking, too. |
I also can't deploy on netlify with edge functions. |
That would work but not sure if it would be an acceptable solution for @natemoo-re. It might be as simple as commenting out this line https://github.com/natemoo-re/astro-icon/blob/main/packages/core/lib/utils.ts#L83 for now but not sure if we'd need to go and actually remove all the code for it to get it to run properly. |
I endup using the library without the svg compression for the moment, |
Yeah I also went for a workaround for now, and created a RootLayout.astro with |
But would would prefer to use this package just for the sake of simplicity |
Same problem deploying to deno. |
bumping this up! |
I have the same problem with SSR on Netlify. |
I think we should separate the svg compression , and each server library (netlify/deno....) should implement its own compression library, we should add just an interface for compression |
Is there a workaround for the time being? |
* Build: Switch to static Vercel deployment This change is required to work around natemoo-re/astro-icon#35.
* Chore: Move from astro-icon to iconify-icon This change is required to work around natemoo-re/astro-icon#35.
This definitely seems like something we should work to improve upon. I'm not sure what the best route is yet but did notice that there is a browser version that we could import which should allow it to work with most environments. |
I was looking into this and think we could utilize the browser version of |
I tried to fork astro-icon to use the browser version before, but was unsuccessful. If you can figure out how to get that to work, then yes! |
Will be working to shift |
Figured out a simple workaround (thanks to @simpleauthority 's comment). We need to patch the I'm using pnpm and it has built-in patching feature. (Note: yarn v2+ also has built-in patching feature, for npm you could use https://github.com/ds300/patch-package) Step 1: run Step 2: run Step 3: open the code /tmp/24c156128bab6fdefc8998671638ad98 & step 4: edit // change line 5 from this
import { optimize as optimizeSVGNative } from "svgo";
// to this
import { optimize as optimizeSVGNative } from "svgo/dist/svgo.browser"; Step 5: commit the patch using # example (adjust the folder path)
pnpm patch-commit /tmp/24c156128bab6fdefc8998671638ad98 Done Run the build command UpdateAlthough |
Looked into this today and it seems there will need to be some effort put into getting this to SSR properly in non-node environments. Will be working on this as a top priority. |
I tried the patch @rupampoddar suggested, but I am getting this error what is the svgo version that you are using? it looks if I remove svgo from the dependencies, it works |
Hi @demiro , Here's my import { defineConfig } from "astro/config";
import cloudflare from "@astrojs/cloudflare";
export default defineConfig({
output: "server",
integrations: [
// ...
],
adapter: cloudflare({
mode: "directory",
}),
vite: {
define: {
// ...
},
ssr: {
external: ["svgo"], // <=== THIS
},
},
}); Update on the patchCurrently my patched file ( /// <reference types="vite/client" />
import { SPRITESHEET_NAMESPACE } from "./constants";
import { Props, Optimize } from "./Props";
import getFromService from "./resolver";
// import { optimize as optimizeSVGNative } from "svgo"; // <==== (PATCH) comment out or remove line
// Adapted from https://github.com/developit/htmlParser
const splitAttrsTokenizer = /([a-z0-9_\:\-]*)\s*?=\s*?(['"]?)(.*?)\2\s+/gim;
const domParserTokenizer =
/(?:<(\/?)([a-zA-Z][a-zA-Z0-9\:]*)(?:\s([^>]*?))?((?:\s*\/)?)>|(<\!\-\-)([\s\S]*?)(\-\->)|(<\!\[CDATA\[)([\s\S]*?)(\]\]>))/gm;
const splitAttrs = (str) => {
let res = {};
let token;
if (str) {
splitAttrsTokenizer.lastIndex = 0;
str = " " + (str || "") + " ";
while ((token = splitAttrsTokenizer.exec(str))) {
res[token[1]] = token[3];
}
}
return res;
};
// (PATCH) update this function
function optimizeSvg(
contents: string,
name: string,
options: Optimize
): string {
return contents; // <==== return content without doing anything,
} PNPM & Cloudflare PagesWhen using pnpm, you also need to make some changes to the build configuration on cloudflare. Follow this : https://community.cloudflare.com/t/add-pnpm-to-pre-installed-cloudflare-pages-tools/288514/5 |
@rupampoddar Thank you for your efforts! I can confirm that your solution works with local svg's too :) |
Uhm is this still failing for anyone else using pnpm with vercel? |
@rupampoddar thank you, so much. This is working for me with Vercel, and also for local icons |
Thanks very helpful |
Here is a way without using pnpm patch, you can configure the vite compilation option of astro.config.mjs import { defineConfig } from "astro/config";
import cloudflare from "@astrojs/cloudflare";
// https://astro.build/config
export default defineConfig({
output: "server",
adapter: cloudflare(),
vite: {
resolve: {
alias: {
"svgo": import.meta.env.PROD ? "svgo/dist/svgo.browser.js" : "svgo"
}
}
}
}); |
Feel free to try out the next release which has support for SSR and svgo v3! You can try it out with:
and read how to use it here: https://github.com/natemoo-re/astro-icon/blob/v1/packages/core/README.md |
Great thanks!!! this workaround works. |
You are a life saver. Thank you! |
removed astro-icon since Cloudflare setup requires hybrid setup, but astro-icon uses svgo which relies on native nodejs modules not available in Cloudflare workers. See natemoo-re/astro-icon#35 .
didnt work for me, when building the project it errors the following |
This also didn't worked for me. Still getting the Any clue in how to solve this? |
svgo
is a Node.js-only package, thus, it doesn't work in the brand-new environments that Astro SSR supports (e.g. Netlify/Vercel Edge, Deno, Cloudflare workers).Is there any other option that supports more environments? Or there could be a way to disable svgo altogether so Astro can build successfully?
The text was updated successfully, but these errors were encountered: