diff options
author | 2022-09-21 15:21:21 -0400 | |
---|---|---|
committer | 2022-09-21 15:21:21 -0400 | |
commit | 5e46be54683592773e6dfc2d33825493886114b0 (patch) | |
tree | 6a9ed635e986ae12037f13c47afe47c260f3bdf2 /packages/integrations/preact/server.js | |
parent | baae1b3fd10cf0a74e880c0e0552ba8d58f24453 (diff) | |
download | astro-5e46be54683592773e6dfc2d33825493886114b0.tar.gz astro-5e46be54683592773e6dfc2d33825493886114b0.tar.zst astro-5e46be54683592773e6dfc2d33825493886114b0.zip |
Support shared signals in Preact islands (#4763)
* Support signals in Preact islands
* Add a changeset
* Only add signals if we need them
* Refactor signal logic into its own module
* Keep track of the signals used
Diffstat (limited to 'packages/integrations/preact/server.js')
-rw-r--r-- | packages/integrations/preact/server.js | 110 |
1 files changed, 0 insertions, 110 deletions
diff --git a/packages/integrations/preact/server.js b/packages/integrations/preact/server.js deleted file mode 100644 index f5b1a34e5..000000000 --- a/packages/integrations/preact/server.js +++ /dev/null @@ -1,110 +0,0 @@ -import { h, Component as BaseComponent } from 'preact'; -import render from 'preact-render-to-string'; -import StaticHtml from './static-html.js'; - -const slotName = (str) => str.trim().replace(/[-_]([a-z])/g, (_, w) => w.toUpperCase()); - -let originalConsoleError; -let consoleFilterRefs = 0; - -function check(Component, props, children) { - if (typeof Component !== 'function') return false; - - if (Component.prototype != null && typeof Component.prototype.render === 'function') { - return BaseComponent.isPrototypeOf(Component); - } - - useConsoleFilter(); - - try { - try { - const { html } = renderToStaticMarkup(Component, props, children); - if (typeof html !== 'string') { - return false; - } - - // There are edge cases (SolidJS) where Preact *might* render a string, - // but components would be <undefined></undefined> - - return !/\<undefined\>/.test(html); - } catch (err) { - return false; - } - } finally { - finishUsingConsoleFilter(); - } -} - -function renderToStaticMarkup(Component, props, { default: children, ...slotted }) { - const slots = {}; - for (const [key, value] of Object.entries(slotted)) { - const name = slotName(key); - slots[name] = h(StaticHtml, { value, name }); - } - // Note: create newProps to avoid mutating `props` before they are serialized - const newProps = { ...props, ...slots }; - const html = render( - h(Component, newProps, children != null ? h(StaticHtml, { value: children }) : children) - ); - return { html }; -} - -/** - * Reduces console noise by filtering known non-problematic errors. - * - * Performs reference counting to allow parallel usage from async code. - * - * To stop filtering, please ensure that there always is a matching call - * to `finishUsingConsoleFilter` afterwards. - */ -function useConsoleFilter() { - consoleFilterRefs++; - - if (!originalConsoleError) { - // eslint-disable-next-line no-console - originalConsoleError = console.error; - - try { - // eslint-disable-next-line no-console - console.error = filteredConsoleError; - } catch (error) { - // If we're unable to hook `console.error`, just accept it - } - } -} - -/** - * Indicates that the filter installed by `useConsoleFilter` - * is no longer needed by the calling code. - */ -function finishUsingConsoleFilter() { - consoleFilterRefs--; - - // Note: Instead of reverting `console.error` back to the original - // when the reference counter reaches 0, we leave our hook installed - // to prevent potential race conditions once `check` is made async -} - -/** - * Hook/wrapper function for the global `console.error` function. - * - * Ignores known non-problematic errors while any code is using the console filter. - * Otherwise, simply forwards all arguments to the original function. - */ -function filteredConsoleError(msg, ...rest) { - if (consoleFilterRefs > 0 && typeof msg === 'string') { - // In `check`, we attempt to render JSX components through Preact. - // When attempting this on a React component, React may output - // the following error, which we can safely filter out: - const isKnownReactHookError = - msg.includes('Warning: Invalid hook call.') && - msg.includes('https://reactjs.org/link/invalid-hook-call'); - if (isKnownReactHookError) return; - } - originalConsoleError(msg, ...rest); -} - -export default { - check, - renderToStaticMarkup, -}; |