jekyll / jekyll-redirect-from

:twisted_rightwards_arrows: Seamlessly specify multiple redirections URLs for your pages and posts.
MIT License
781 stars 112 forks source link

run javascript at first to avoid splash #158

Closed NewFuture closed 6 years ago

NewFuture commented 7 years ago

Just changed the order of the scripts. In most browsers (such as chrome), it will execute the javascript before page rendering. So that the page will redirect without the splash white screen :)

NewFuture commented 7 years ago

Of course. But it will parse the refresh meta firstly, which is unnecessary when the JavaScript is enabled. We just want to it redirect as soon as possible 😄

pathawks commented 6 years ago

It seems the <meta> should be parsed first, and the JavaScript only serve as a backup incase something weird happens. Is that not what is happening?

NewFuture commented 6 years ago

Not exactly.
The script should be paresed at first and the <meta> is a back.

Most of the browsers will parse and execute the inline JavaScript as document order. This JavaScript code will be executed immediately when parsed. Then it the browser will redirect and stop to parse and render the HTML document.

For an example

Try this code in Chrome

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script>
        alert(1);
        location="https://github.com/";
    </script>
    <title>Title</title>
    <meta http-equiv="refresh" content="0; url=http://jekyllrb.com/">
    <script>
        alert(2);
    </script>
</html>

It will alert and redirect to https://github.com/ immediately; and it will not show the Title or execute the second JavaScript bock (alert(2)) or redirect ro http://jekyllrb.com/.

For current version

It will display a screen like this screenshot, before jumping to new URL. image

For this PR

It will not show this screen unless the JavaScript is disabled (the <meta> is a backup). If the network is slow it will just show the Redirecting… in browser title (without painting the window). I think this this way it better for both user experience and performance.

DirtyF commented 6 years ago

@jekyllbot: merge +minor