React和Django缓存问题
我有一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来好像React正在拦截链接的
OnClick
事件,并防止浏览器正常处理。您可以尝试更改链接标签,以防止这样做:
有没有一种方法可以排除React 它们应该更改为使用我上面说明的格式,或者如果您根本不希望它们显示。
您的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:
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.
Your nginx configuration is written correctly. You can test it here.
我想我找到了解决方案。
1-将您的React-Scripts更新为 ^V4
2-添加您不想被缓存到service-worker.js文件的URL:
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: