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

fix(server-renderer): respect compilerOptions during runtime template compilation #4631

Merged
merged 2 commits into from Sep 25, 2021

Conversation

skirtles-code
Copy link
Contributor

@skirtles-code skirtles-code commented Sep 19, 2021

Currently, only the deprecated form of isCustomElement is respected when performing runtime template compilation with SSR. All other compiler options are ignored. e.g.:

import { createApp } from "vue"
import { renderToString } from "@vue/server-renderer"

const app = createApp({
  template: `<mjml></mjml>`
})

// This works, but it is deprecated and no other compilerOptions are respected
app.config.isCustomElement = tag => tag.startsWith('mj')

;(async () => {
  const markup = await renderToString(app, {})
  console.log(markup)
})()

This PR attempts to use compilerOptions consistently with other forms of template compilation.

Unresolved problems:

  • The cache does not take the options into consideration. It was already ignoring isCustomElement and I didn't want to expand the scope of this PR. Now fixed.
  • The logic to resolve the options is duplicated with runtime-core/src/component.ts. Extracting the logic to a shared function should be a fairly painless refactor.
  • The deprecated forms of isCustomElement and delimiters do not show warnings during SSR.

@@ -24,29 +24,48 @@ export function ssrCompile(
)
}

// TODO: The cache does not take the compilerOptions into account
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The runtime compiler options are limited and largely serializable. We can call fn.toString() if isCustomElement is present, and isNativeTag is in fact constant since server-renderer implies runtime-dom.

So we can resolve the options first, serialize it to get a cache key that takes options into account.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I think that should be working now.

There are some potential edge cases around using fn.toString(), e.g. if someone uses a utility function to create isCustomElement functions they will likely have the same toString but different values caught in their closures. This probably isn't worth worrying about given the templates also have to be a perfect match. If someone did encounter that extreme edge case, it should be possible for them to work around it by passing a cache-buster in the compilerOptions.

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

Successfully merging this pull request may close these issues.

None yet

2 participants