diff --git a/.changeset/orange-buttons-bow.md b/.changeset/orange-buttons-bow.md new file mode 100644 index 000000000..54a912b9a --- /dev/null +++ b/.changeset/orange-buttons-bow.md @@ -0,0 +1,8 @@ +--- +'@emotion/cache': patch +--- + +author: @danieldelcore +author: @mitchellhamilton + +Fixed moving of client-side inserted style tags from Emotion 10 when intending to hydrate Emotion 11 styles resulting in losing styles in production diff --git a/packages/cache/__tests__/hydration.js b/packages/cache/__tests__/hydration.js index 2119d48f2..132f26237 100644 --- a/packages/cache/__tests__/hydration.js +++ b/packages/cache/__tests__/hydration.js @@ -35,3 +35,149 @@ test('rehydrated styles to head can be flushed', () => { cache.sheet.flush() expect(document.documentElement).toMatchSnapshot() }) + +test('flushing rehydrated styles in the head only affect styles matching the cache key', () => { + safeQuerySelector('head').innerHTML = [ + '', + '' + ].join('') + + // this moves emotion style tags at initialization time + jest.resetModules() + require('@emotion/react') + + const cache = createCache({ key: 'emo' }) + expect(document.documentElement).toMatchInlineSnapshot(` + + + + + + + + `) + + cache.sheet.flush() + expect(document.documentElement).toMatchInlineSnapshot(` + + + + + + + `) +}) + +test('should only hydrate style elements matching the cache key', () => { + let css = `color:hotpink;` + let hash = hashString(css) + + safeQuerySelector( + 'body' + ).innerHTML = `` + + const cache = createCache({ key: 'custom-key' }) + + expect(cache.inserted).toEqual({}) + expect(document.documentElement).toMatchInlineSnapshot(` + + + + + + + `) + + const cache2 = createCache({ key: 'emo' }) + + expect(cache2.inserted).toEqual({ [hash]: true }) + expect(document.documentElement).toMatchInlineSnapshot(` + + + + + + + `) +}) + +test('Existing client-side inserted styles from Emotion 10 should not be moved', () => { + // the nested nature isn't special, it's just meant to be a general "make sure they're not moved" + safeQuerySelector( + 'body' + ).innerHTML = `
` + expect(document.documentElement).toMatchInlineSnapshot(` + + + +
+ ` + safeQuerySelector('body').appendChild(thing) + jest.resetModules() + require('@emotion/react') + + expect(document.documentElement).toMatchInlineSnapshot(` + + + + + +
+