gpt4 book ai didi

reactjs - 在 Next.js 中使用 redux-persist 时元标记出现问题

转载 作者:行者123 更新时间:2023-12-02 01:32:29 25 4
gpt4 key购买 nike

我正在使用 Next.js 和 redux 构建一个电子商务应用程序。我注意到,当我将 redux-persist 添加到应用程序时,所有元标记都停止工作。我在下面添加了我的代码片段

import { combineReducers } from 'redux'
import { configureStore } from '@reduxjs/toolkit'
import shopReducer from './reducers/shop/shopSlice'
import storage from 'redux-persist/lib/storage'
import { persistStore,persistReducer, FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER } from 'redux-persist'

const initState = {}

const persistConfig = {
key: 'root',
version: 1,
storage,
whitelist: ['shop'],
}

const rootReducer = combineReducers({
shop: shopReducer,
})

const persistedReducer = persistReducer(persistConfig, rootReducer)

const reduxStore = () => {
const store = configureStore ({
initState,
reducer: persistedReducer,
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({
serializableCheck: {
ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
},
}),
})

const persistor = persistStore(store)
return { persistor, store }
}

export default reduxStore

然后在_app.js

import Layout from '../components/Layout'
import { Provider } from 'react-redux'
import reduxStore from '../context/store'
import { PersistGate } from 'redux-persist/integration/react'

const { persistor, store } = reduxStore()

function MyApp({ Component, pageProps }){
return (
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<Layout>
<Component {...pageProps} />
</Layout>
</PersistGate>
</Provider>
)
}

export default MyApp

meta tags 组件是这样的,不包括其他标签。

import Head from 'next/head'

function Meta({ title, keywords, description, image, appName, url }){
return (
<Head>
<meta name="theme-color" content="#0066b2" />
<meta name="description" content={description} />
<meta name="keywords" content={keywords} />
<meta name="author" content="author" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:title" content={title} />
<meta name="twitter:description" content={description} />
<meta property="twitter:image" content={image} />
<meta property="twitter:image:alt" content={appName} />
<meta name="msapplication-TileColor" content="#ffffff" />
<title>{title}</title>
</Head>
)
}

Meta.defaultProps = {
appName: 'Store',
url: 'https://example.com',
title: 'Store Example',
keywords: 'ecommerce',
description: 'Store',
image: '/storefront.png',
}

export default Meta

我该如何解决这个问题,因为我想同时使用 redux-persist 来保存访客购物车和用于 SEO 的元标记?

谢谢

最佳答案

我找到了关于这个 Github link 的解决方案.显然 redux-persist 存在问题,它禁用了 SSR,这基本上就是为什么元标记在使用 redux-persist 时不起作用的原因。

解决办法是改下面的代码

<PersistGate persistor={persistor} loading={null}>
<Layout>
<Component {...pageProps} />
</Layout>
</PersistGate>

对此

<PersistGate persistor={persistor} loading={null}>
{() => (
<Layout>
<Component {...pageProps} />
</Layout>
)}
</PersistGate>

关于reactjs - 在 Next.js 中使用 redux-persist 时元标记出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72966026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com