当前位置:网站首页>In a single-page app, what is the right way to deal with wrong URLs (404 errors)?

In a single-page app, what is the right way to deal with wrong URLs (404 errors)?

2022-06-25 06:36:00 javail

problem :

I am currently writing a web application using angularjs, but I think this question applies to any client-side javascript framework that does routing on the client side ( as angular does ). I am currently using angularjs Write a Web Applications , But I think this problem applies to any client that routes on the client javascript frame ( It's like angular equally ).

In a single-page app, what is the right way to deal with wrong URLs? In a single page application , Handling errors URL What's the right way ?

Looking at a few major sites, I see that gmail will redirect to the inbox if you type any random URL below https://mail.google.com/mail/ . Check out several major sites , I found that if you were https://mail.google.com/mail/ Next Type any random URL,gmail Redirect to inbox .This happens server-side (with an http 300 code) or client-side, depending on whether the wrong path is before or after the # character. This happens on the server side ( Use http 300 Code ) Or client , It depends on the error path # Before or after characters .On the other hand, twitter shows a real HTTP 404 for any invalid URL. On the other hand ,twitter Any invalid... Will be displayed URL The real HTTP 404.A third option would be to show a "soft" 404, a purely client-side error page. The third option is to display “ soft ”404, A pure client error page .

These solutions seem appropriate for different situations. These solutions seem to work in different situations .Twitter wants the links to twitter users and tweets to be real links, so people can share them, post them in news articles, etc, so it is important that invalid links be recognized as such (if I have a broken link to a tweet in my website, a simple crawl will tell me that).Twitter hope Twitter Links between users and tweets are real links , So people can share them , Post them in news articles , Therefore, it is important to identify invalid links ( If I am on my website , A simple crawl would tell me ).In gmail, on the other hand, you are not expected to share links into your inbox, and I'm not even sure if the links are really permanent/persistent: it seems the url updating mostly serves the purpose of browser history navigation within the single-page app. On the other hand , stay gmail in , You won't share links to your inbox , I'm not even sure if these links are really permanent / lasting : Seems to be url Updates are mainly used for browser history navigation single page applications .The third approach of giving soft errors might be appropriate for situations similar to gmail, but where there is no reasonable "default" page. The third method of giving soft errors may be applicable to things like gmail The situation of , But there is no reasonable “ Default ” page .

After this long introduction, here are some specific questions: After such a long introduction , Here are some specific questions :

  • Is it ever acceptable to give a "soft" error page instead of a 404 error, or should a single-page app always redirect to a real 404 if a url is invalid? Provide “ soft ” Error page instead of 404 Whether the error is acceptable , Or if url Invalid , Whether a single page application should always redirect to the real 404?
  • Gmail's code may be perfectly bugfree, but if it did have a bug leading to invalid links that end up redirecting back to the inbox, that might be even more confusing for users than an error page.Gmail Your code may be completely error free , But if it does have errors that cause invalid links to eventually redirect the recycle bin , So for users , This may be more confusing than the error page .For most web apps out there, that are not as well tested as gmail, would it be better to show an error page? For most web applications , They don't look like gmail That's fully tested , Would it be better to display an error page ?
  • To implement real 404s for single-page apps, it seems necessary to duplicate the routing logic on the server-side. To achieve real... For single page applications 404, It seems necessary to replicate the routing logic on the server side .Is there any way around this? Is there any way to solve ?
  • When redirecting to a 404, I think the user should be able to see the URL that caused the error, possibly in the URL bar. When redirecting to 404 when , I think the user should be able to see the URL, May be in URL In the bar .With the html5 history api, I think this can be accomplished by simply triggering a reload of the current page (with the wrong url), combined with the server-side routing mentioned above. Use html5 history api, I think this can be done by simply triggering a reload of the current page ( Use the wrong url), In combination with the server-side routing mentioned above .For browsers that do not support this or when using hashbang notation, this does not seem possible. For not supporting this function or using hashbang Symbol browser , It seems impossible .What's the best way to support all browsers? What is the best way to support all browsers ?

Solution :

Reference resources : https://stackoom.com/en/question/100k2
原网站

版权声明
本文为[javail]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202201234286968.html