|
1 | 1 | import util from '../../shared/util.ts'
|
2 | 2 |
|
| 3 | +export const clientStyles = new Map<string, string>() |
3 | 4 | export const serverStyles = new Map<string, string>()
|
4 |
| -export const recoverStyles = new Map<string, string>() |
5 | 5 |
|
6 |
| -export function removeCSS(id: string, recoverable?: boolean) { |
| 6 | +export function removeCSS(url: string, recoverable?: boolean) { |
7 | 7 | const { document } = window as any
|
8 | 8 | Array.from(document.head.children).forEach((el: any) => {
|
9 |
| - if (el.getAttribute('data-module-id') === id) { |
| 9 | + if (el.getAttribute('data-module-id') === url) { |
10 | 10 | if (recoverable) {
|
11 |
| - recoverStyles.set(id, el.innerHTML) |
| 11 | + const tag = el.tagName.toLowerCase() |
| 12 | + if (tag === 'style') { |
| 13 | + clientStyles.set(url, el.innerHTML) |
| 14 | + } else if (tag === 'link') { |
| 15 | + clientStyles.set(url, '') |
| 16 | + } |
12 | 17 | }
|
13 | 18 | document.head.removeChild(el)
|
14 | 19 | }
|
15 | 20 | })
|
16 | 21 | }
|
17 | 22 |
|
18 |
| -export function recoverCSS(id: string) { |
19 |
| - if (recoverStyles.has(id)) { |
20 |
| - applyCSS(id, recoverStyles.get(id)!) |
| 23 | +export function recoverCSS(url: string) { |
| 24 | + if (clientStyles.has(url)) { |
| 25 | + const css = clientStyles.get(url)! |
| 26 | + if (css === '' && util.isLikelyHttpURL(url)) { |
| 27 | + applyCSS(url) |
| 28 | + } else { |
| 29 | + applyCSS(url, css) |
| 30 | + } |
21 | 31 | }
|
22 | 32 | }
|
23 | 33 |
|
24 |
| -export function applyCSS(id: string, css: string) { |
| 34 | +export function applyCSS(url: string, css?: string) { |
25 | 35 | if (util.inDeno) {
|
26 |
| - serverStyles.set(id, css) |
| 36 | + serverStyles.set(url, css || '') |
27 | 37 | } else {
|
28 | 38 | const { document } = window as any
|
29 |
| - const ssrStyle = Array.from<any>(document.head.children).find((el: any) => { |
30 |
| - return el.getAttribute('data-module-id') === id && el.hasAttribute('ssr') |
| 39 | + const ssr = Array.from<any>(document.head.children).find((el: any) => { |
| 40 | + return el.getAttribute('data-module-id') === url && el.hasAttribute('ssr') |
31 | 41 | })
|
32 |
| - if (ssrStyle) { |
33 |
| - ssrStyle.removeAttribute('ssr') |
| 42 | + if (ssr) { |
| 43 | + // apply the css at next time |
| 44 | + ssr.removeAttribute('ssr') |
34 | 45 | } else {
|
35 |
| - const prevStyleEls = Array.from(document.head.children).filter((el: any) => { |
36 |
| - return el.getAttribute('data-module-id') === id |
| 46 | + const prevEls = Array.from(document.head.children).filter((el: any) => { |
| 47 | + return el.getAttribute('data-module-id') === url |
37 | 48 | })
|
38 |
| - const styleEl = document.createElement('style') |
39 |
| - styleEl.type = 'text/css' |
40 |
| - styleEl.appendChild(document.createTextNode(css)) |
41 |
| - styleEl.setAttribute('data-module-id', id) |
42 |
| - document.head.appendChild(styleEl) |
43 |
| - if (prevStyleEls.length > 0) { |
44 |
| - prevStyleEls.forEach(el => document.head.removeChild(el)) |
| 49 | + let el: any |
| 50 | + if (css !== undefined) { |
| 51 | + el = document.createElement('style') |
| 52 | + el.type = 'text/css' |
| 53 | + el.appendChild(document.createTextNode(css)) |
| 54 | + } else if (util.isLikelyHttpURL(url)) { |
| 55 | + el = document.createElement('link') |
| 56 | + el.rel = 'stylesheet' |
| 57 | + el.href = url |
| 58 | + } else { |
| 59 | + throw new Error('applyCSS: missing css') |
| 60 | + } |
| 61 | + el.setAttribute('data-module-id', url) |
| 62 | + document.head.appendChild(el) |
| 63 | + if (prevEls.length > 0) { |
| 64 | + prevEls.forEach(el => document.head.removeChild(el)) |
45 | 65 | }
|
46 | 66 | }
|
47 | 67 | }
|
|
0 commit comments