提交 aaaaa38a 编写于 作者: fxy060608's avatar fxy060608

feat(ssr): support title

上级 22ff4d84
......@@ -3,7 +3,7 @@
{
"path": "pages/index/index",
"style": {
"navigationBarTitleText": "uni-app"
"navigationBarTitleText": "ssr"
}
}
],
......@@ -13,4 +13,4 @@
"navigationBarBackgroundColor": "#F8F8F8",
"backgroundColor": "#F8F8F8"
}
}
}
......@@ -117,7 +117,7 @@ const onError = /*#__PURE__*/ createHook(ON_ERROR);
const onThemeChange = /*#__PURE__*/ createHook(ON_THEME_CHANGE);
const onPageNotFound = /*#__PURE__*/ createHook(ON_PAGE_NOT_FOUND);
const onUnhandledRejection = /*#__PURE__*/ createHook(ON_UNHANDLE_REJECTION);
// export const onLoad = /*#__PURE__*/ createHook(ON_LOAD)
const onReady = /*#__PURE__*/ createHook(ON_READY);
const onUnload = /*#__PURE__*/ createHook(ON_UNLOAD);
const onResize = /*#__PURE__*/ createHook(ON_RESIZE);
......
......@@ -82,7 +82,7 @@ const onError = /*#__PURE__*/ createHook(ON_ERROR);
const onThemeChange = /*#__PURE__*/ createHook(ON_THEME_CHANGE);
const onPageNotFound = /*#__PURE__*/ createHook(ON_PAGE_NOT_FOUND);
const onUnhandledRejection = /*#__PURE__*/ createHook(ON_UNHANDLE_REJECTION);
// export const onLoad = /*#__PURE__*/ createHook(ON_LOAD)
const onReady = /*#__PURE__*/ createHook(ON_READY);
const onUnload = /*#__PURE__*/ createHook(ON_UNLOAD);
const onResize = /*#__PURE__*/ createHook(ON_RESIZE);
......
......@@ -10866,8 +10866,11 @@ function createRightWindowTsx(rightWindow, layoutState, windowState) {
}
}
function useDocumentTitle(pageMeta) {
const ctx = vue.useSSRContext();
function update() {
document.title = pageMeta.navigationBar.titleText;
{
ctx[uniShared.UNI_SSR_TITLE] = pageMeta.navigationBar.titleText;
}
}
vue.watchEffect(update);
}
......
import {isFunction, extend, hyphenate, isPlainObject, isString, isArray, hasOwn, isObject, capitalize, toRawType, makeMap as makeMap$1, isPromise, invokeArrayFns as invokeArrayFns$1} from "@vue/shared";
import {injectHook, withModifiers, createVNode, getCurrentInstance, inject, provide, reactive, openBlock, createBlock, mergeProps, toDisplayString, defineComponent, ref, computed, watch, onUnmounted, onBeforeUnmount, onActivated, onMounted, nextTick, onBeforeMount, withDirectives, vShow, shallowRef, watchEffect, isVNode, Fragment, markRaw, createTextVNode, onBeforeActivate, onBeforeDeactivate, renderList, onDeactivated, Teleport, createApp, Transition, withCtx, KeepAlive, resolveDynamicComponent, resolveComponent, createCommentVNode, renderSlot} from "vue";
import {injectHook, withModifiers, createVNode, getCurrentInstance, inject, provide, reactive, openBlock, createBlock, mergeProps, toDisplayString, defineComponent, ref, computed, watch, onUnmounted, onBeforeUnmount, onActivated, onMounted, nextTick, onBeforeMount, withDirectives, vShow, shallowRef, watchEffect, isVNode, Fragment, markRaw, createTextVNode, onBeforeActivate, onBeforeDeactivate, renderList, onDeactivated, Teleport, createApp, Transition, withCtx, KeepAlive, resolveDynamicComponent, resolveComponent, createCommentVNode, renderSlot, useSSRContext} from "vue";
import {once, passive, normalizeTarget, isBuiltInComponent, initCustomDataset, invokeArrayFns, NAVBAR_HEIGHT, parseQuery, PRIMARY_COLOR, debounce, getCustomDataset, callOptions, removeLeadingSlash, getLen, ON_REACH_BOTTOM_DISTANCE, decodedQuery, updateElementStyle, addFont, scrollTo, RESPONSIVE_MIN_WIDTH, formatDateTime} from "@dcloudio/uni-shared";
import {initVueI18n, LOCALE_EN, LOCALE_ES, LOCALE_FR, LOCALE_ZH_HANS, LOCALE_ZH_HANT} from "@dcloudio/uni-i18n";
import {useRoute, createRouter, createWebHistory, createWebHashHistory, useRouter, isNavigationFailure, RouterView} from "vue-router";
......@@ -15766,6 +15766,7 @@ function useKeyboard() {
if (res) {
key.value = res;
}
nextTick(() => key.value = "");
};
onMounted(() => {
document.addEventListener("keyup", onKeyup);
......@@ -19967,8 +19968,11 @@ const UniServiceJSBridge$1 = /* @__PURE__ */ extend(ServiceJSBridge, {
}
});
function useDocumentTitle(pageMeta) {
useSSRContext();
function update() {
document.title = pageMeta.navigationBar.titleText;
{
document.title = pageMeta.navigationBar.titleText;
}
}
watchEffect(update);
onActivated(update);
......
import { watchEffect, onActivated } from 'vue'
import { watchEffect, onActivated, useSSRContext } from 'vue'
import { UNI_SSR_TITLE } from '@dcloudio/uni-shared'
export function useDocumentTitle(pageMeta: UniApp.PageRouteMeta) {
const ctx = useSSRContext()
function update() {
document.title = pageMeta.navigationBar.titleText!
if (__NODE_JS__) {
ctx![UNI_SSR_TITLE] = pageMeta.navigationBar.titleText
} else {
document.title = pageMeta.navigationBar.titleText!
}
}
watchEffect(update)
onActivated(update)
......
......@@ -382,6 +382,7 @@ const RESPONSIVE_MIN_WIDTH = 768;
const COMPONENT_NAME_PREFIX = 'VUni';
const PRIMARY_COLOR = '#007aff';
const UNI_SSR = '__uniSSR';
const UNI_SSR_TITLE = 'title';
const UNI_SSR_STORE = 'store';
const UNI_SSR_DATA = 'data';
const UNI_SSR_GLOBAL_DATA = 'globalData';
......@@ -407,6 +408,7 @@ exports.UNI_SSR = UNI_SSR;
exports.UNI_SSR_DATA = UNI_SSR_DATA;
exports.UNI_SSR_GLOBAL_DATA = UNI_SSR_GLOBAL_DATA;
exports.UNI_SSR_STORE = UNI_SSR_STORE;
exports.UNI_SSR_TITLE = UNI_SSR_TITLE;
exports.addFont = addFont;
exports.callOptions = callOptions;
exports.createRpx2Unit = createRpx2Unit;
......
......@@ -131,6 +131,8 @@ export declare const UNI_SSR_GLOBAL_DATA = "globalData";
export declare const UNI_SSR_STORE = "store";
export declare const UNI_SSR_TITLE = "title";
export declare function updateElementStyle(element: HTMLElement, styles: Partial<CSSStyleDeclaration>): void;
export { }
......@@ -378,6 +378,7 @@ const RESPONSIVE_MIN_WIDTH = 768;
const COMPONENT_NAME_PREFIX = 'VUni';
const PRIMARY_COLOR = '#007aff';
const UNI_SSR = '__uniSSR';
const UNI_SSR_TITLE = 'title';
const UNI_SSR_STORE = 'store';
const UNI_SSR_DATA = 'data';
const UNI_SSR_GLOBAL_DATA = 'globalData';
......@@ -388,4 +389,4 @@ function getEnvLocale() {
return (lang && lang.replace(/[.:].*/, '')) || 'en';
}
export { BUILT_IN_TAGS, COMPONENT_NAME_PREFIX, COMPONENT_PREFIX, COMPONENT_SELECTOR_PREFIX, NAVBAR_HEIGHT, ON_REACH_BOTTOM_DISTANCE, PLUS_RE, PRIMARY_COLOR, RESPONSIVE_MIN_WIDTH, TABBAR_HEIGHT, TAGS, UNI_SSR, UNI_SSR_DATA, UNI_SSR_GLOBAL_DATA, UNI_SSR_STORE, addFont, callOptions, createRpx2Unit, debounce, decode, decodedQuery, defaultRpx2Unit, formatDateTime, getCustomDataset, getEnvLocale, getLen, initCustomDataset, invokeArrayFns, isBuiltInComponent, isCustomElement, isNativeTag, normalizeDataset, normalizeTarget, once, parseQuery, passive, plusReady, removeLeadingSlash, sanitise, scrollTo, stringifyQuery, updateElementStyle };
export { BUILT_IN_TAGS, COMPONENT_NAME_PREFIX, COMPONENT_PREFIX, COMPONENT_SELECTOR_PREFIX, NAVBAR_HEIGHT, ON_REACH_BOTTOM_DISTANCE, PLUS_RE, PRIMARY_COLOR, RESPONSIVE_MIN_WIDTH, TABBAR_HEIGHT, TAGS, UNI_SSR, UNI_SSR_DATA, UNI_SSR_GLOBAL_DATA, UNI_SSR_STORE, UNI_SSR_TITLE, addFont, callOptions, createRpx2Unit, debounce, decode, decodedQuery, defaultRpx2Unit, formatDateTime, getCustomDataset, getEnvLocale, getLen, initCustomDataset, invokeArrayFns, isBuiltInComponent, isCustomElement, isNativeTag, normalizeDataset, normalizeTarget, once, parseQuery, passive, plusReady, removeLeadingSlash, sanitise, scrollTo, stringifyQuery, updateElementStyle };
......@@ -8,6 +8,7 @@ export const COMPONENT_NAME_PREFIX = 'VUni'
export const PRIMARY_COLOR = '#007aff'
export const UNI_SSR = '__uniSSR'
export const UNI_SSR_TITLE = 'title'
export const UNI_SSR_STORE = 'store'
export const UNI_SSR_DATA = 'data'
export const UNI_SSR_GLOBAL_DATA = 'globalData'
......@@ -4,6 +4,7 @@ import {
UNI_SSR,
UNI_SSR_DATA,
UNI_SSR_STORE,
UNI_SSR_TITLE,
UNI_SSR_GLOBAL_DATA,
} from '@dcloudio/uni-shared'
import { plugin } from '@dcloudio/uni-h5'
......@@ -39,7 +40,7 @@ export async function render(url, manifest = {}) {
__uniSSR[UNI_SSR_STORE] = store.state
}
const appContext = renderAppContext(ctx)
return [html, preloadLinks, appContext]
return [html, preloadLinks, appContext, ctx[UNI_SSR_TITLE] || '']
}
function renderPreloadLinks(modules, manifest) {
......
......@@ -63,9 +63,10 @@ export async function createSSRServer(options: CliOptions & ServerOptions) {
)
).render
const [appHtml, preloadLinks, appContext] = await render(url)
const [appHtml, preloadLinks, appContext, title] = await render(url)
const html = template
.replace(/<title>(.*?)<\/title>/, `<title>${title}</title>`)
.replace(`<!--preload-links-->`, preloadLinks)
.replace(`<!--app-html-->`, appHtml)
.replace(`<!--app-context-->`, appContext)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册