React和Django缓存问题

发布于 2025-02-04 07:34:49 字数 700 浏览 2 评论 0原文

我有一个React和Django应用程序,该应用程序正在NGINX后面提供。 /admin路线和 /API都指向UWSGI。但是,当加载这些路由时,除非对页面的硬刷新进行刻度,否则将提供React App。似乎React正在服务所有路线,而不仅仅是索引。

有没有办法排除React中的路由,因此仅在路径为“/”或Nginx/django配置中有某些内容时,它才能显示,我可以更改以克服此问题。

这是我的nginx conf的片段:

    location / {
        try_files $uri $uri/ =404;
    }
    location /api/ {
        uwsgi_pass  uwsgi;
        include     /etc/nginx/uwsgi_params;
    }
    location /admin/ {
        uwsgi_pass  uwsgi;
        include     /etc/nginx/uwsgi_params;
    }

还有我的django urls config:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/v1/', include(routers.urls))
]

关于我如何进行的任何想法,都将不胜感激

I have a react and django app which is being served behind nginx. The /admin route and /api routes both point to uwsgi. However when loading these routes the react app is served unless a hard refresh of the page is peformed. It seems like react is serving all routes instead of just the index.

Is there a way to exclude routes in react so it will only display if the path is "/" or is there something in nginx/django config I can change to overcome this issue.

This is a snippet from my nginx conf:

    location / {
        try_files $uri $uri/ =404;
    }
    location /api/ {
        uwsgi_pass  uwsgi;
        include     /etc/nginx/uwsgi_params;
    }
    location /admin/ {
        uwsgi_pass  uwsgi;
        include     /etc/nginx/uwsgi_params;
    }

and my django urls config:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/v1/', include(routers.urls))
]

Any ideas on how I could proceed would be appreciated

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

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

发布评论

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

评论(2

小兔几 2025-02-11 07:34:49

听起来好像React正在拦截链接的OnClick事件,并防止浏览器正常处理。

您可以尝试更改链接标签,以防止这样做:

<a href="/admin/" onClick={() => { location.href = "/admin/" }}>Admin</a>

/* Or if using react router */
<Link to="/admin/" onClick={() => { location.href = "/admin/" }}>Admin</Link>

是否有一种方法来排除React中的路由,因此仅在路径为“/”…

的情况下显示出来。

有没有一种方法可以排除React 它们应该更改为使用我上面说明的格式,或者如果您根本不希望它们显示。

…或nginx/django config中是否有一些东西,我可以更改以克服此问题。

您的nginx配置正确编写。您可以在此处进行测试

It sounds like React is intercepting the onClick event of the links and preventing the browser from handling it normally.

You can try altering the link tags so that they prevent React from doing that:

<a href="/admin/" onClick={() => { location.href = "/admin/" }}>Admin</a>

/* Or if using react router */
<Link to="/admin/" onClick={() => { location.href = "/admin/" }}>Admin</Link>

Is there a way to exclude routes in react so it will only display if the path is "/"…

How are the routes added or configured in React? They should be changed to use the format I illustrated above, or deleted if you don't want them to display at all.

… or is there something in nginx/django config I can change to overcome this issue.

Your nginx configuration is written correctly. You can test it here.

护你周全 2025-02-11 07:34:49

我想我找到了解决方案。

1-将您的React-Scripts更新为 ^V4

2-添加您不想被缓存到service-worker.js文件的URL:

registerRoute(
   ({ request, url }) => {

      ...

      if (
         url.pathname.startsWith('/admin') ||
         url.pathname.startsWith('/api')
      ) {
         return false;
      }

      ...

      return true;
   },
   createHandlerBoundToURL(process.env.PUBLIC_URL + '/index.html')
);

I think i found the solution.

1- update your react-scripts to ^v4

2- add urls you don't want to be cached to service-worker.js file:

registerRoute(
   ({ request, url }) => {

      ...

      if (
         url.pathname.startsWith('/admin') ||
         url.pathname.startsWith('/api')
      ) {
         return false;
      }

      ...

      return true;
   },
   createHandlerBoundToURL(process.env.PUBLIC_URL + '/index.html')
);

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