Reloading the page gives wrong GET request with AngularJS HTML5 mode

前端 未结 24 2960
慢半拍i
慢半拍i 2020-11-22 01:39

I want to enable HTML5 mode for my app. I have put the following code for the configuration, as shown here:

return app.config([\'$routeProvider\',\'$location         


        
相关标签:
24条回答
  • 2020-11-22 01:56

    I believe your issue is with regards to the server. The angular documentation with regards to HTML5 mode (at the link in your question) states:

    Server side Using this mode requires URL rewriting on server side, basically you have to rewrite all your links to entry point of your application (e.g. index.html)

    I believe you'll need to setup a url rewrite from /about to /.

    0 讨论(0)
  • 2020-11-22 01:56

    Gulp + browserSync:

    Install connect-history-api-fallback via npm, later config your serve gulp task

    var historyApiFallback = require('connect-history-api-fallback');
    
    gulp.task('serve', function() {
      browserSync.init({
        proxy: {
                target: 'localhost:' + port,
                middleware: [ historyApiFallback() ]
            }
      });
    });
    
    0 讨论(0)
  • 2020-11-22 01:57

    I am using apache (xampp) on my dev environment and apache on the production, add:

    errorDocument 404 /index.html
    

    to the .htaccess solve for me this issue.

    0 讨论(0)
  • 2020-11-22 01:58

    Finally I got a way to to solve this issue by server side as it's more like an issue with AngularJs itself I am using 1.5 Angularjs and I got same issue on reload the page. But after adding below code in my server.js file it is save my day but it's not a proper solution or not a good way .

    app.use(function(req, res, next){
      var d = res.status(404);
         if(d){
            res.sendfile('index.html');
         }
    });
    
    0 讨论(0)
  • 2020-11-22 01:59

    IIS URL Rewrite Rule to prevent 404 error after page refresh in html5mode

    For angular running under IIS on Windows

    <rewrite>
      <rules>
        <rule name="AngularJS" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="/" />
        </rule>
      </rules>
    </rewrite>
    

    NodeJS / ExpressJS Routes to prevent 404 error after page refresh in html5mode

    For angular running under Node/Express

    var express = require('express');
    var path = require('path');
    var router = express.Router();
    
    // serve angular front end files from root path
    router.use('/', express.static('app', { redirect: false }));
    
    // rewrite virtual urls to angular app to enable refreshing of internal pages
    router.get('*', function (req, res, next) {
        res.sendFile(path.resolve('app/index.html'));
    });
    
    module.exports = router;
    

    More info at: AngularJS - Enable HTML5 Mode Page Refresh Without 404 Errors in NodeJS and IIS

    0 讨论(0)
  • 2020-11-22 02:00

    From the angular docs

    Server side
    Using this mode requires URL rewriting on server side, basically you have to rewrite all your links to entry point of your application (e.g. index.html)

    The reason for this is that when you first visit the page (/about), e.g. after a refresh, the browser has no way of knowing that this isn't a real URL, so it goes ahead and loads it. However if you have loaded up the root page first, and all the javascript code, then when you navigate to /about Angular can get in there before the browser tries to hit the server and handle it accordingly

    0 讨论(0)
提交回复
热议问题