浏览代码

Add `data-instant` attribute to whitelist a link with a query string

Alexandre Dieulot 6 年之前
父节点
当前提交
cc475a5f74
共有 3 个文件被更改,包括 19 次插入8 次删除
  1. 1 1
      instantpage.js
  2. 18 6
      test/app.js
  3. 0 1
      test/header.html

+ 1 - 1
instantpage.js

@@ -98,7 +98,7 @@ function isPreloadable(linkElement) {
     return
   }
 
-  if (!allowQueryString && urlObject.search) {
+  if (!allowQueryString && urlObject.search && !('instant' in linkElement.dataset)) {
     return
   }
 

+ 18 - 6
test/app.js

@@ -6,15 +6,19 @@ const crypto = require('crypto')
 const sleep = require('util').promisify(setTimeout)
 
 const argvIndexOfFile = process.argv.indexOf(__filename)
-let SLEEP_TIME = parseInt(process.argv[argvIndexOfFile + 1])
+let DATA_INSTANT = parseInt(process.argv[argvIndexOfFile + 1])
+if (isNaN(DATA_INSTANT)) {
+  DATA_INSTANT = 0
+}
+let SLEEP_TIME = parseInt(process.argv[argvIndexOfFile + 2])
 if (isNaN(SLEEP_TIME)) {
   SLEEP_TIME = 200
 }
-let CACHE_MAX_AGE = parseInt(process.argv[argvIndexOfFile + 2])
+let CACHE_MAX_AGE = parseInt(process.argv[argvIndexOfFile + 3])
 if (isNaN(CACHE_MAX_AGE)) {
   CACHE_MAX_AGE = 0
 }
-let PORT = parseInt(process.argv[argvIndexOfFile + 3])
+let PORT = parseInt(process.argv[argvIndexOfFile + 4])
 if (isNaN(PORT)) {
   PORT = 8000
 }
@@ -52,16 +56,24 @@ async function requestListener(req, res) {
       headers['Cache-Control'] = `max-age=${CACHE_MAX_AGE}`
     }
 
+    if (DATA_INSTANT) {
+      content += `<body>`
+    }
+    else {
+      content += `<body data-instant-allow-query-string>`
+    }
+    dataInstantAttribute = DATA_INSTANT ? `data-instant` : ``
+
     content += await fsPromises.readFile(path.resolve(__dirname, 'header.html'))
     content += `<h1>Page ${page}</h1>`
     for (let i = 1; i <= 3; i++) {
       if (page != i) {
-        content += `<a href="/${i}?${Math.random()}"><span>Page ${i}</span></a>`
+        content += `<a href="/${i}?${Math.random()}" ${dataInstantAttribute}><span>Page ${i}</span></a>`
       }
     }
 
-    content += `<a href="/${page}?${Math.random()}" target="_blank"><span>Opens in a new tab</span></a>`
-    content += `<a href="/${page}?${Math.random()}#anchor"><span>Other page anchor</span></a>`
+    content += `<a href="/${page}?${Math.random()}" target="_blank" ${dataInstantAttribute}><span>Opens in a new tab</span></a>`
+    content += `<a href="/${page}?${Math.random()}#anchor" ${dataInstantAttribute}><span>Other page anchor</span></a>`
     content += `<a href="${req.url}#anchor" id="anchor"><span>Same-page anchor</span></a>`
     content += `<a href="/${page}?${Math.random()}" data-no-instant><span>Manually blacklisted link</span></a>`
     content += `<a href="https://www.google.com/"><span>External link</span></a>`

+ 0 - 1
test/header.html

@@ -29,4 +29,3 @@ a:active {
   background: hsla(0, 0%, 0%, .25);
 }
 </style>
-<body data-instant-allow-query-string>