所以我使用的一些软件包不能通用。我想知道是否有一种方法可以让我们只能在服务器上加载特定的模块。
目前,根据官方存储库,您可以使用此技术检查您是否在组件内的浏览器或服务器中:
constructor(@Inject(PLATFORM_ID) private platformId: Object) { ... }
...
if (isPlatformBrowser(this.platformId)) {...}
Run Code Online (Sandbox Code Playgroud)
但是,如果我可以通过某种全局变量检查当前我们是在浏览器还是服务器中,那么选择要加载的模块将变得微不足道。
不确定这是渲染服务器端的功能还是副作用,但每当我刷新页面或直接从 url 输入访问页面时,存储在公开的 _app.js 页面中的任何状态都会丢失。Next.js 声明 _app.js 页面用于“导航页面时保持状态”,因此这应该可行。
当使用组件路由客户端时,所有状态都会保留<Link>。
我试图在不使用 cookie/会话/本地存储的情况下存储非敏感数据。
任何人都可以验证这是否是最好的方法,还是我应该只使用其中一种技术?
如有必要,很乐意发布代码。
已解决 --- 请阅读更新 2
你好,我在使用 React-leaflet 地图的 React 入门套件中遇到此错误
它就像 github 模块示例,但我不知道问题是什么!我认为SSR有问题
React 版本:16.x 和 React 入门套件
更新1
ReferenceError: window is not defined
at D:\project\react-starterkit\node_modules\leaflet\src\core\Util.js:217:24
at version (D:\project\react-starterkit\node_modules\leaflet\dist\leaflet-src.js:7:65)
at Object.<anonymous> (D:\project\react-starterkit\node_modules\leaflet\dist\leaflet-src.js:10:2)
at Module._compile (module.js:635:30)
at Module._extensions..js (module.js:646:10)
at Object.require.extensions.(anonymous function) [as .js] (D:\project\react-starterkit\node_modules\babel-register\lib\node.js:152:7)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Module.require (module.js:579:17)
Run Code Online (Sandbox Code Playgroud)
通过更新反应解决最新版本 16.x
TypeError: (0 , _react.createContext) is not a function
at Object.<anonymous> (D:\project\react-starterkit\node_modules\react-leaflet\lib\context.js:18:47)
at Module._compile (module.js:635:30)
at Module._extensions..js (module.js:646:10)
at Object.require.extensions.(anonymous function) [as .js] (D:\project\react-starterkit\node_modules\babel-register\lib\node.js:152:7) …Run Code Online (Sandbox Code Playgroud) reactjs server-side-rendering react-starter-kit react-leaflet
我已经使用 command 完成了项目构建ng build --prod。它成功生成构建,但此构建无法在实时服务器上运行,它给出以下错误:-
main.0769518151819531d924.js:1 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'hasOwnProperty' of undefined
TypeError: Cannot read property 'hasOwnProperty' of undefined
at v (main.0769518151819531d924.js:1)
at Mf (main.0769518151819531d924.js:1)
at t.get (main.0769518151819531d924.js:1)
at kt (main.0769518151819531d924.js:1)
at Pt (main.0769518151819531d924.js:1)
at Et (main.0769518151819531d924.js:1)
at main.0769518151819531d924.js:1
at main.0769518151819531d924.js:1
at Df (main.0769518151819531d924.js:1)
at Mf (main.0769518151819531d924.js:1)
at P (polyfills.7d30aceca8fe7a5b6974.js:1)
at P (polyfills.7d30aceca8fe7a5b6974.js:1)
at polyfills.7d30aceca8fe7a5b6974.js:1
at e.invokeTask (polyfills.7d30aceca8fe7a5b6974.js:1)
at Object.onInvokeTask (main.0769518151819531d924.js:1)
at e.invokeTask (polyfills.7d30aceca8fe7a5b6974.js:1)
at t.runTask (polyfills.7d30aceca8fe7a5b6974.js:1)
at g (polyfills.7d30aceca8fe7a5b6974.js:1)
at t.invokeTask …Run Code Online (Sandbox Code Playgroud) 目标
我想Authorization用访问令牌填充我的标头。我想将该访问令牌存储在 Apollo 缓存中,因为Auth0明确声明不要将访问令牌存储在本地存储中(我不知道为什么Apollo 客户端文档似乎认为这是可以的)。
如果做不到这一点,我想安全地存储我的访问令牌,并能够将其添加到 Apollo 服务器的每个 Apollo 客户端请求中。
const apolloClient = withApollo(({ctx, headers, initialState}) => {
const cache = new InMemoryCache();
// Code here to access local cache.
return new ApolloClient({
cache,
link: new HttpLink({
uri: <apollo server endpoint>,
headers: {
Authorization: `Bearer ${accessToken}`,
'Access-Control-Allow-Origin': 'http://localhost:3000/',
...headers
},
credentials: 'include'
}),
typeDefs,
resolvers
})
})
class MyApp extends App {
render() {
const { Component, pageProps, apollo } = this.props;
return ( …Run Code Online (Sandbox Code Playgroud) authorization apollo server-side-rendering react-apollo next.js
问题是如何在服务器端对用户进行身份验证?我很快就听说身份验证应该在客户端使用isPlatformBrowser. 但问题是当用户登录时,然后我们向 Angular ssr 发送请求,这里渲染页面不正确,用户将看到登录页面,而不是看到正确的页面!我想知道没有人提到这个问题!还推断出两种方式: 1:在服务器端,我们应该向 api 服务器请求并获得批准用户是否有效。2:我们使用cookie并检查authGuard上的cookie中是否有特定的密钥
我想知道还有其他方法比我的更好吗?并且还想知道我是否使用 cookie 方法,仅检查其是否存在并且除了 api 服务器之外没有人可以手动添加它是否安全?
我使用 Github 的 API 来获取固定存储库的列表,并将调用放入 AsyncData 方法中,以便在第一次渲染时获得该列表。但我刚刚了解到 AsyncData 在服务器端调用一次,然后每次在客户端加载页面时调用。这意味着客户端不再拥有进行 API 调用的令牌,无论如何,我不会让我的 Github 令牌出现在客户端中。
当我切换页面(从另一个页面到带有列表的页面)时,数据不存在,我只有默认的空数组
我不知道确保我的数据始终加载在服务器端的最佳方法是什么?
export default defineComponent({
name: 'Index',
components: { GithubProject, Socials },
asyncData(context: Context) {
return context.$axios.$post<Query<UserPinnedRepositoriesQuery>>('https://api.github.com/graphql', {
query,
}, {
headers: {
// Token is defined on the server, but not on the client
Authorization: `bearer ${process.env.GITHUB_TOKEN}`,
},
})
.then((data) => ({ projects: data.data.user.pinnedItems.nodes }))
.catch(() => {});
},
setup() {
const projects = ref<Repository[]>([]);
return {
projects,
};
},
});
Run Code Online (Sandbox Code Playgroud) 我有<MobileLayout />,<DesktopLayout />。我用于Next.js服务器端渲染。我注意到有许多著名的 ui 库都有移动检测组件,例如<Respnosive />Semantic-UI-React 中的组件。但所有这些都是客户端方法,无法正常工作SSR
我阅读了一些文档,结论是我应该检查user-agent服务器端req.headers。在 Next.js 中,检测设备并有条件渲染MobileLayout/之一的正确方法是什么DesktopLayout?
我尝试过的
在_app.js
import isMobile from 'ismobilejs'
...
function Homepage({ Component, pageProps, mobile }){
return (
mobile ?
<MobileLayout><Component {...pageProps} /></MobileLayout> :
<DesktopLayout><Component {...pageProps} /></DesktopLayout>
)
}
HomePage.getInitialProps = async (appContext) => {
const userAgent = appContext.ctx.req.headers['user-agent']
const mobile = isMobile(userAgent).any
const appProps = await App.getInitialProps(appContext)
return { ...appProps, mobile }
}
Run Code Online (Sandbox Code Playgroud)
但问题是 …
在将服务器端渲染React应用程序迁移到可加载组件以进行代码拆分和延迟加载后,初始包大小及其下载时间按预期减少。然而,在将经典的React渲染方法替换为Loadable Components 之后,在其余应用程序代码不变的情况下,我在PageSpeed / LightHouse 中的累积布局移位得分飙升,从 0.05 到 1 或更高。我的意思是即使没有动态加载任何组件,否则情况会更糟。
我做错了什么?
可加载组件之前的 SSR 代码(良好的 CLS 分数):
服务器端 :
...
import React from 'react'
import { renderToString } from 'react-dom/server'
import { StaticRouter } from 'react-router-dom'
...
const ssrApp = renderToString(
<StaticRouter location={`/${this.requestedPage.uri}`} context={{}}>
<App />
</StaticRouter>
)
Run Code Online (Sandbox Code Playgroud)
客户端 :
...
import React from 'react'
import { BrowserRouter } from 'react-router-dom'
import { hydrate } from …Run Code Online (Sandbox Code Playgroud) 我在apollo-client repo 中发布了这个,但我想我也会问 stackoverflow
你好!我是阿波罗(以及整个 graphql)的新手,并且对 SSR / SSG 有一些疑问。我从概念上知道 SSR/SSG 是什么以及它是如何工作的,但对 apollo-client 不太了解。
我已经尝试在网上搜索和搜索正确执行此操作的方法,并且看到了两个版本而几乎没有解释原因,所以我这篇文章的目标是有一个指向和去的地方“这就是为什么你做一个其他”。
做之间有什么好处/坏处
这是 TypeScript 和伪代码的混合体,请不要批评语法 kthx
// apolloClient.ts
const client = new ApolloClient({
link: new HttpLink({ uri: '/graphql' }),
cache: new InMemoryCache(),
});
Run Code Online (Sandbox Code Playgroud)
// component.tsx
import client from '../apolloClient';
const Component: FunctionalComponent = ({ data }) => {
...
}
export const getServerSideProps: GetServerSideProps = async () => {
const { data } = client.query(...);
return {
props: { data }
}
} …Run Code Online (Sandbox Code Playgroud) next.js ×4
reactjs ×4
angular ×3
apollo ×1
build ×1
javascript ×1
node.js ×1
nuxt.js ×1
react-apollo ×1
state ×1
typescript ×1
vue.js ×1