Skip to content
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

skipEffects flag value does not return to original value #63

Open
mo36924 opened this issue Sep 12, 2024 · 1 comment
Open

skipEffects flag value does not return to original value #63

mo36924 opened this issue Sep 12, 2024 · 1 comment

Comments

@mo36924
Copy link

mo36924 commented Sep 12, 2024

Throw a Promise in a function component with the Suspense.
SkipEffects flag value does not return to original value.

As an example, the following code causes an error.

import { equal } from "node:assert";
import { options } from "preact";
import { createElement, lazy, Suspense } from "preact/compat";
import prepass from "preact-ssr-prepass";

const skipEffects = "__s";
const Component = lazy(() => Promise.resolve({ default: () => null }));
const App = () => createElement(Suspense, { fallback: null }, createElement(Component, {}));
equal(options[skipEffects], undefined);

prepass(createElement(App, {})).then(() => {
  // This assertion fails because the `skipEffects` option is set to `true` in the `preact-ssr-prepass` module.
  equal(options[skipEffects], undefined);
});
@mo36924
Copy link
Author

mo36924 commented Sep 12, 2024

try {
const previousSkipEffects = options[_skipEffects];
options[_skipEffects] = true;
// options.render was renamed to _render (mangled to __r)
if (options.render) options.render(vnode);
if (options.__r) {
options.__r(vnode);
}
const renderResult = Promise.resolve(
nodeName.call(vnode.__c, props, cctx)
);
options[_skipEffects] = previousSkipEffects;
return renderResult;
} catch (e) {
if (e && e.then) {
return e.then(doRender, doRender);
}
return Promise.reject(e);
}

It might be better to return the value of the flag when an error is thrown.

const previousSkipEffects = options[_skipEffects]; 
options[_skipEffects] = true;
try {
  ...
} catch(e) {
  ...
} finally {
  options[_skipEffects] = previousSkipEffects; 
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant