Skip to content
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

Fix error in server-side rendering (SSR) #2039

Merged
merged 1 commit into from
Nov 26, 2019
Merged

Conversation

Livven
Copy link
Contributor

@Livven Livven commented Oct 17, 2019

A similar PR may already be submitted!
Please search among the Pull request before creating one.

Thanks for submitting a pull request! Please provide enough information so that others can review your pull request:

Before opening a pull request, please make sure all the tests pass locally by running npm test.

Summary

This PR fixes/implements the following bugs/features

Explain the motivation for making this change. What existing problem does the pull request solve?

html2canvas causes JavaScript applications that use server-side rendering (e.g. via Next.js, Nuxt, Gatsby, Gridsome) to break as the CacheStorage.setContext(window) call that is executed on import tries to access the window variable that does not exist in Node.

Obviously, this won't fix the actual functionality of taking a screenshot in Node, but it will allow for apps that use SRR to be pre-rendered on the server and then delivered to the client, where it will then be possible to use this library take screenshots.

Test plan (required)

Demonstrate how the issue/feature can be replicated. For most cases, simply adding an appropriate html/css template into the reftests should be sufficient. Please see other tests there for reference.

Code formatting

Please make sure that code adheres to the project code formatting. Running npm run format will automatically format your code correctly.

Closing issues

Fixes #

@gorkemyontem
Copy link

Is there any expected merge date for this?

@thomasMary
Copy link

@niklasvh could you merge this one 🙏

@Livven
Copy link
Contributor Author

Livven commented Nov 12, 2019

@gorkemyontem @thomasMary FYI you can use e.g. the awesome patch-package package to patch the offending code out yourself without having to fork the project. It works pretty well and is what we've been doing in lieu of an official fix.

@niklasvh niklasvh merged commit e4d52a1 into niklasvh:master Nov 26, 2019
@machadogj
Copy link

hi @niklasvh, I can confirm that this fix seems to work, but it's hasn't been published to npm yet. anyway we can get it in npm soon? thanks!

@christianstrang
Copy link

This answer worked for me: https://stackoverflow.com/a/59540525/90257

@americool
Copy link

Is this up on npm yet?

@esteban89
Copy link

Is this up on npm yet?

It seems not, any idea how to fix this error?

@worteepz
Copy link

Hello everyone! An error occurs with server side rendering (the window is not defined), how can I update npm module from this pull ?

In the released version v1.0.0-rc.5, there is no this update, it is insanely important for our project!

@Livven

@esteban89
Copy link

esteban89 commented May 14, 2020

Hello everyone! An error occurs with server side rendering (the window is not defined), how can I update npm module from this pull ?

In the released version v1.0.0-rc.5, there is no this update, it is insanely important for our project!

@Livven

I updated it manually in html2canvas.js and changed it from:
CacheStorage.setContext(window);
to
if (typeof window !== "undefined") { CacheStorage.setContext(window); }

and that worked, at least for me

@machadogj
Copy link

machadogj commented May 14, 2020 via email

@worteepz
Copy link

Hello everyone! An error occurs with server side rendering (the window is not defined), how can I update npm module from this pull ?
In the released version v1.0.0-rc.5, there is no this update, it is insanely important for our project!
@Livven

I updated it manually in html2canvas.js and changed it from:
CacheStorage.setContext(window);
to
if (typeof window !== "undefined") { CacheStorage.setContext(window); }

and that worked, at least for me

Thx for your reply! You edit this in node_modules folder ? Or you load the html2canvas script directly in project folder without npm and edit them ?

@worteepz
Copy link

You might want to take a look at dynamic imports with no ssr:

Thx for your reply! In suddenly, i don't use Next.js, in project we use Gatsby.js. I use this method, but it not working for me :(

@esteban89
Copy link

Hello everyone! An error occurs with server side rendering (the window is not defined), how can I update npm module from this pull ?
In the released version v1.0.0-rc.5, there is no this update, it is insanely important for our project!
@Livven

I updated it manually in html2canvas.js and changed it from:
CacheStorage.setContext(window);
to
if (typeof window !== "undefined") { CacheStorage.setContext(window); }
and that worked, at least for me

Thx for your reply! You edit this in node_modules folder ? Or you load the html2canvas script directly in project folder without npm and edit them ?

Yes I edited it in node_modules folder

@gaurav-pantelwar
Copy link

gaurav-pantelwar commented May 16, 2020

Hi @esteban89 @worteepz,

for nextjs,
If you are getting > ReferenceError: window is not defined error even after using dynamic imports with no ssr as mentioned by @machadogj, then it may be due to the dependency which requiring window be present and is imported at the top level.

If you remove your import:

import html2canvas from 'html2canvas';  // <-- Remove this line

Usage:

// Use Something like this

async functionToUseHtml2Canvas () {
   const html2canvas = await import("html2canvas") // <-- Import asynchronously
   html2canvas(document.body).then(function(canvas) {
        document.body.appendChild(canvas);
    })
}

Hope it Helps 🙏

I have not tested html2canvas library but some other library which causing the same > ReferenceError: window is not defined error in my nextjs project.
It might work for SSR other than nextjs.

@maxamorim
Copy link

@niklasvh any plans to release this fix on npm?

@nlevchuk
Copy link

Please bump to new version

oliveira-gust pushed a commit to oliveira-gust/html2canvas that referenced this pull request May 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.