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

Ensuring that masonry will only be initialized on the client side (SSR fix) #1121

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

cliqqz
Copy link

@cliqqz cliqqz commented Jan 14, 2020

Since JS frameworks / libraries + SSR (Server-Server-Rendering) got pretty popular it would be great to make this plugin work with SSR projects. This condition ensures that the masonry plugin will only load within the browser and not on the server and eventually crash it by resulting in a ReferenceError for the window object.

an older reference issue I found where other people experienced this: #966

…n using it within SSR projects

Since JS frameworks / libraries + SSR (Server-Server-Rendering) got pretty popular it would be great to make this plugin work with SSR projects. This condition ensures that the masonry plugin will only load within the browser and not on the server and eventually crash it then.
@desandro
Copy link
Owner

Thanks for this contribution. I think there is a better solution to load import masory-layout when you're in a SSR environment.

@drewbaker
Copy link

I would Masonry to have not throw window not defined errors when used with import. Yes you can work around this with a require() but now with the popularity of Vite that doesn't work out of the box either. So this PR would have been a nice solution I think.

@Xyaren
Copy link

Xyaren commented Oct 24, 2023

This is also blocking me from using SSR. Please consider this PR.

@jvyden
Copy link

jvyden commented Oct 24, 2023

For those looking for a workaround for Angular SSR while this PR becomes official, you can tell NPM to pull this version directly from git if you specify the repository and commit hash in your package.json:

    "masonry-layout": "github:cliqqz/masonry#ffd2b614237176fd184b70b90a4559f9716dd29c",

I believe you also need to modify your package-lock as well for changes to take effect. You can view my implementation here: https://github.com/LittleBigRefresh/refresh-web/blob/master/package.json

Copy link

@jvyden jvyden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving as I've had this working in production for a couple months now. Pretty sure this repository is just unmaintained, though.

Our best bet is native support for masonry grids in CSS to stop being experimental: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout/Masonry_Layout

@Xyaren
Copy link

Xyaren commented Oct 24, 2023

For those looking for a workaround for Angular SSR while this PR becomes official, you can tell NPM to pull this version directly from git if you specify the repository and commit hash in your package.json:

    "masonry-layout": "github:cliqqz/masonry#ffd2b614237176fd184b70b90a4559f9716dd29c",

I believe you also need to modify your package-lock as well for changes to take effect. You can view my implementation here: https://github.com/LittleBigRefresh/refresh-web/blob/master/package.json

As I am using it as a peerDependency of ngx-masonry I was able to define an overwrite like this:

...
  "dependencies": {...},
  "devDependencies": {...},
  "overrides": {
    "ngx-masonry": {
      "masonry-layout": "github:cliqqz/masonry#ffd2b614237176fd184b70b90a4559f9716dd29c"
    },
...

https://docs.npmjs.com/cli/v8/configuring-npm/package-json#overrides

This requires a non-ancient npm version though.

erickgcastro

This comment was marked as duplicate.

@leblancmeneses
Copy link

alternative solution given this is a 1 line fix is to use npx patch-package masonry-layout after you update node_modules/masonry-layout with what this pr has.

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.

8 participants