如何防止路由器。在使用NextJS和GetServersideProps时,ASPATH使用效应在负载上呈现两次?

发布于 2025-02-12 04:27:08 字数 2141 浏览 2 评论 0原文

URL查询参数(带有GetServersIdeProps的NextJS页面)构建探索页面

  1. 如果外部用户进入此url domain.com/explore?type = actions&category= food,我将使用 “操作”和“食物”的数据
  2. 如果内部用户使用页面过滤器,它将生成新的URL domain.com/explore?然后,我获取数据并渲染。

为此,我基本上是在[Router.aspath]依赖项上设置使用效率。问题在于,外部用户的负载两次(由于Gerserversideprops?),因此两次获取数据:(

有提示吗?谢谢!

useEffect(() => {
        // parsing the url
        const url = location.search
        const urlQuery = url.slice(1)
        const result = {}
        urlQuery.split("&").forEach(part => {
            const item = part.split("=");
            result[item[0]] = decodeURIComponent(item[1]);
        });
        console.log(result)
        
        // Updating forms/filters states and setting up query parameters
        queryParams = [] // reseting the params
        setFiltersData(prevFiltersData => {
            return {
                ...prevFiltersData,
                thumbType: result.type, 
            }
        })
        if (result.field) {
            setFiltersData(prevFiltersData => {
                return {
                    ...prevFiltersData,
                    categoryField: result.field, 
                    categoryOperator: result.fieldOp, 
                    categoryValue: result.fieldVal,
                }
            })
            queryParams.push(where(result.field, result.fieldOp, decodeURIComponent(result.fieldVal)))
        }
        if (result.orderBy) {
            setFiltersData(prevFiltersData => {
                return {
                    ...prevFiltersData,
                    orderFieldActions: result.orderBy, 
                    orderOperatorActions: result.orderType,
                }
            })
            queryParams.push(orderBy(result.orderBy, result.orderType))
        }
        setSearchParams(queryParams) // saving query params to state for subsequent data fetch
        getFilteredData(result.type) // Fetching data from the DB
        setInitialLoading(false)
    }, [router.asPath])

I am building an explore page with url query parameters (NextJs page with getServerSideProps)

  1. If an external user goes onto this url domain.com/explore?type=actions&category=food it will fetch on the DB the data for "actions" and "food"
  2. If an internal user uses on-page filters, it generates a new url domain.com/explore?type=actions&category=food&orderBy=points and I then fetch the data and render.

To do so, I am basically setting up a useEffect with the [router.asPath] dependency. The problem is that it renders twice on load for external users (due to gerServerSideProps ?) and therefore fetching the data twice :(

Any hints ? Thanks !

useEffect(() => {
        // parsing the url
        const url = location.search
        const urlQuery = url.slice(1)
        const result = {}
        urlQuery.split("&").forEach(part => {
            const item = part.split("=");
            result[item[0]] = decodeURIComponent(item[1]);
        });
        console.log(result)
        
        // Updating forms/filters states and setting up query parameters
        queryParams = [] // reseting the params
        setFiltersData(prevFiltersData => {
            return {
                ...prevFiltersData,
                thumbType: result.type, 
            }
        })
        if (result.field) {
            setFiltersData(prevFiltersData => {
                return {
                    ...prevFiltersData,
                    categoryField: result.field, 
                    categoryOperator: result.fieldOp, 
                    categoryValue: result.fieldVal,
                }
            })
            queryParams.push(where(result.field, result.fieldOp, decodeURIComponent(result.fieldVal)))
        }
        if (result.orderBy) {
            setFiltersData(prevFiltersData => {
                return {
                    ...prevFiltersData,
                    orderFieldActions: result.orderBy, 
                    orderOperatorActions: result.orderType,
                }
            })
            queryParams.push(orderBy(result.orderBy, result.orderType))
        }
        setSearchParams(queryParams) // saving query params to state for subsequent data fetch
        getFilteredData(result.type) // Fetching data from the DB
        setInitialLoading(false)
    }, [router.asPath])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

分开我的手 2025-02-19 04:27:08

终于找到了使用此线程的解决方案。
问题是由于在Next.config.js中使用的“严格模式”中的反应。
https://reactjs.org/docs/strict-mode.html

解决方案:

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: false,
  experimental: {
    scrollRestoration: true,
  },
  images: {
    domains: ['lh3.googleusercontent.com', 'graph.facebook.com', 'firebasestorage.googleapis.com'],
  },
}

module.exports = nextConfig

将严格模式切换为false

Finally found a solution with this thread. https://github.com/vercel/next.js/issues/35822
The problem is due to React being used in "Strict mode" in the next.config.js.
https://reactjs.org/docs/strict-mode.html

Solution :

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: false,
  experimental: {
    scrollRestoration: true,
  },
  images: {
    domains: ['lh3.googleusercontent.com', 'graph.facebook.com', 'firebasestorage.googleapis.com'],
  },
}

module.exports = nextConfig

Switching the strictMode to false

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文