-
-
Notifications
You must be signed in to change notification settings - Fork 83
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
.display and .toString different behavior on nextjs in dev and build mode #260
Comments
This sounds very similar to an issue we had recently and fixed in #239 |
Pretty shure. |
@LeaVerou any update on this? Facing same issue. |
Hey, sorry this slipped through the cracks. Could you make a reduced testcase that I could look at and play with (e.g. on stackblitz or something)? |
Can confirm, having this issue too, albeit slightly differently. let color = new Color('hsl(0 100% 50%)')
color.to('p3').toString() // Dev = color(display-p3 0.9175 0.2003 0.1386)
color.to('p3').toString() // Prod = color(p3 0.9175 0.2003 0.1386) Works fine for other formats I've found 🤔 |
I took a look at this, the reduced test case was super helpful. When the test app is run in dev mode the code for the page is run in the browser and works correctly. In production mode ( The color.js code that is failing is the assignment of the Lines 26 to 28 in b896c0d
If you print out the value of the format variable (pay attention to the location of the format = color.space.getFormat(format)
?? color.space.getFormat("default")
?? ColorSpace.DEFAULT_FORMAT;
inGamut ||= format.toGamut;
let coords = color.coords.slice(); // clone so we can manipulate it
if (inGamut && !checkInGamut(color)) {
// FIXME what happens if the color contains NaNs?
coords = toGamut(clone(color), inGamut === true ? undefined : inGamut).coords;
}
console.log(`format is ${format}`); If you move the format = color.space.getFormat(format)
?? color.space.getFormat("default")
?? ColorSpace.DEFAULT_FORMAT;
console.log(`format is ${format}`);
inGamut ||= format.toGamut; If you access a property of format = color.space.getFormat(format)
?? color.space.getFormat("default")
?? ColorSpace.DEFAULT_FORMAT;
let x = format.type; If you replace the nullish coalescing operators with if/else statements the code runs correctly. format = color.space.getFormat(format);
if (!format) {
format = color.space.getFormat("default");
}
else {
if (!format) {
format = ColorSpace.DEFAULT_FORMAT;
}
} This also works and maybe should be the preferred workaround? format = color.space.getFormat(format) ?? color.space.getFormat("default");
if (!format) {
format = ColorSpace.DEFAULT_FORMAT;
} TLDR: The issue appears to be Next.js bug but there are some ways to work around the issue. |
Thank you so much for tracking it down @lloydk! Should we close this issue then? |
Up to you, we can either close the issue or I can create a PR for one of the work arounds. |
Maybe we should have a FAQ listing these sorts of known issues and workarounds? |
Changing the code from inGamut ||= format.toGamut;
let coords = color.coords.slice(); // clone so we can manipulate it to let coords = color.coords.slice(); // clone so we can manipulate it
inGamut ||= format.toGamut; seems to work for me so I'll create a PR for that change. |
Workaround for a Next.js bug when generating a production build of a Next.js project using Color.js. See this Github issue for an analysis of the bug: color-js#260
* Add a workaround for Next.js production build bug Workaround for a Next.js bug when generating a production build of a Next.js project using Color.js. See this Github issue for an analysis of the bug: #260 * Add comment for workaround
Trying to use
color.display({"format": "hex"})
as well as.toString({"format": "hex"})
in nextjs v13.In
dev
it works fine, outputs#111
for example, but inprod
mode it outputscolor(srgb 0.0667 0.0667 0.0667)
when running bundle afternext build
.So I use this method to get the same result in
dev
and inprod
modeColor.defaults.display_space.formats.hex.serialize(color.coords)
.Is there any "correct" solution to output hexadecimal representation of colour (
#123456
)?The text was updated successfully, but these errors were encountered: