-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve runtime context logic (#6898)
- Loading branch information
Showing
17 changed files
with
114 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@modern-js/runtime': patch | ||
'@modern-js/types': patch | ||
--- | ||
|
||
fix: requestContext should be add on runtimeContext before init | ||
fix: requestContext 应该在 init 前添加到 runtimeContext |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/runtime/plugin-runtime/src/core/compat/requestContext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// this plugin is use to provide request context to runtime context | ||
import type { RuntimeContext } from '../context'; | ||
import type { RuntimePluginFuture } from '../plugin/types'; | ||
import type { TSSRContext } from '../types'; | ||
|
||
export const makeRequestContext = (context: RuntimeContext) => { | ||
const baseSSRContext = context.ssrContext; | ||
const requestContext = baseSSRContext | ||
? { | ||
isBrowser: context.isBrowser, | ||
request: baseSSRContext.request || ({} as TSSRContext['request']), | ||
response: baseSSRContext.response || ({} as TSSRContext['response']), | ||
logger: baseSSRContext.logger || ({} as TSSRContext['logger']), | ||
} | ||
: ({} as TSSRContext); | ||
|
||
return requestContext; | ||
}; | ||
|
||
export const requestContextPlugin = (): RuntimePluginFuture => ({ | ||
name: '@modern-js/runtime-plugin-request-context', | ||
|
||
setup(api) { | ||
api.onBeforeRender(context => { | ||
const requestContext = makeRequestContext(context); | ||
context.context = requestContext; | ||
}); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { type RuntimeContext, useRuntimeContext } from '@modern-js/runtime'; | ||
|
||
const App = () => { | ||
const { initialData, context } = useRuntimeContext(); | ||
|
||
console.log(context.request.url, '!!!'); | ||
return ( | ||
<div className="text-center" id="data"> | ||
Hello, Modern.js. env name: {(initialData?.name || '') as string} | ||
</div> | ||
); | ||
}; | ||
|
||
App.init = (context: RuntimeContext) => { | ||
const { request } = context.context!; | ||
|
||
if (context.isBrowser && !context?.initialData?.name) { | ||
return { | ||
name: 'client', | ||
}; | ||
} else if (!request.query.browser) { | ||
return { | ||
name: 'server', | ||
}; | ||
} | ||
|
||
return {}; | ||
}; | ||
|
||
export default App; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"extends": "@modern-js/tsconfig/base", | ||
"compilerOptions": { | ||
"declaration": false, | ||
"jsx": "preserve", | ||
"baseUrl": "./", | ||
"paths": { | ||
"@/*": ["./src/*"], | ||
"@shared/*": ["./shared/*"], | ||
"@api/*": ["./api/*"] | ||
} | ||
}, | ||
"include": ["src", "shared", "config", "modern.config.ts", "api", "server"] | ||
} |