Skip to content

dynamic SSR env #10

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

dynamic SSR env #10

wants to merge 2 commits into from

Conversation

shjyh
Copy link

@shjyh shjyh commented Jan 13, 2024

Dynamically setting env.SSR enables the framework to use different logic codes in SSR and CSR

for example:

// v-ifdef SSR
import db from "./db"
// v-endif

export default function findProduct() {
  // v-ifdef SSR
  return db.findProduct()
  // v-else
  return fetch("/api/product/search).then(res=>res.json())
  // v-endif
}

shjyh added 2 commits January 13, 2024 10:18
Dynamically setting env.SSR enables the framework to use different logic codes in SSR and CSR
@@ -10,7 +10,9 @@ const VitePluginConditionalCompile = (userOptions: UserOptions = {}): Plugin =>
configResolved(config) {
ctx.env = { ...ctx.env, ...config.env }
},
transform: (code, id) => ctx.transform(code, id),
transform: (code, id, SSR) => {
ctx.env.SSR = SSR
Copy link
Owner

Choose a reason for hiding this comment

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

Why is it not like this?

transform: (code, id, { ssr }) => {
    ctx.env.SSR = !!ssr
}

Copy link
Author

Choose a reason for hiding this comment

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

This is my mistake, it should be like this

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

Successfully merging this pull request may close these issues.

2 participants