Fix for window undefined issue with SSR#1194
Open
vamsikrishna4 wants to merge 1 commit into
Open
Conversation
|
solves the problem in svelete, why it is not yet merged 😫😫 |
|
Any updates? |
oriolj
added a commit
to oriolj/masonry-pretext
that referenced
this pull request
Apr 8, 2026
…dev.5, § L.2b)
`import Masonry from 'masonry-pretext'` no longer crashes during
Next.js / Nuxt / SvelteKit / Vite SSR build passes. The fix wraps
every UMD wrapper's `window` reference with
`typeof window !== 'undefined' ? window : {}`
so the bundle can be loaded in a Node `vm` context with empty
globals. The factory bodies' `window.console` / `window.jQuery`
reads then become `undefined` instead of throwing — and the
existing falsy checks already handle that.
Verified by test/visual/ssr-smoke.mjs which loads
dist/masonry.pkgd.min.js in a `node:vm` context with empty
globals and asserts the IIFE doesn't throw. **The test was added
in desandro#4 as a diagnostic that always failed; this commit flips it
to passing and promotes it into `make test` as a permanent gate.**
Patches:
- masonry.js (line 33): direct source edit. UMD call site
wrapped. First source edit in the fork — every previous
improvement went through a build-time plugin.
- scripts/build.mjs:
- new ssrDomGuardPlugin with one entry per dep file:
- outlayer/outlayer.js (UMD call site)
- outlayer/item.js (UMD call site, via the
existing patch plugin's
transform desandro#7 to avoid an
onLoad-filter conflict)
- get-size/get-size.js (UMD call site)
- fizzy-ui-utils/utils.js (UMD call site + a typeof
document guard inside the
docReady function body)
- jquery-bridget/jquery-bridget.js (UMD call site)
- Makefile + package.json: `make test` and `npm test` now run
visual + ssr-smoke. New `npm run test:ssr` for ssr-only.
Closes upstream desandro/masonry:
- desandro#1194 "Fix for window undefined issue with SSR"
- desandro#1121 "Ensuring that masonry will only be initialized on the client side (SSR fix)"
- desandro#1201 "vite build fails"
All three open for 1-2 years with no upstream movement.
Numbers (vs pre-005):
masonry.js source raw 7473 → 7510 (+37 B)
dist/masonry.pkgd.min.js raw 23296 → 23450 (+154 B, +0.66%)
dist/masonry.pkgd.min.js gz 7616 → 7629 ( +13 B, +0.17%)
dist/masonry.pkgd.min.js br 6851 → 6898 ( +47 B, +0.69%)
visual regression tests 4/4 → 4/4
SSR smoke test ✗ → ✓ ← first pass
vs upstream-frozen v4.2.2:
dist/masonry.pkgd.min.js raw 24103 → 23450 (−653 B)
dist/masonry.pkgd.min.js gz 7367 → 7629 (+262 B, of which
+13 B is the SSR
fix; the rest is
the residual desandro#2
esbuild cost)
SSR import works ✗ → ✓ ← first time
The SSR fix took 3 iterations of build-and-test to converge.
Iteration 1 missed the docReady chain (Outlayer.create runs
htmlInit at module load → docReady → document.readyState).
Iteration 2 missed jquery-bridget (forgot it gets bundled via
the entry file's require). Each missing patch was surfaced in
<30 seconds by ssr-smoke.mjs and fixed in another minute. The
methodology converged to a working SSR fix because the gate was
already in place.
Predicted vs actual: all six predictions inside their bands.
Full record: improvements/005-ssr-import-fix.md
Roadmap progress: FORK_ROADMAP.md § Progress (row 4b')
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.