-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Restore smoketest for offline-ready SW page #518
Changes from 4 commits
961eb29
ffa7c12
cf6e085
a2ceda2
329e1a1
f757c17
58de622
7c8a519
cf981ba
0a79219
610c2fd
a7ad4e9
ca04762
7a1cad8
cf0422c
2366ab4
e5be990
83b86da
92fab6f
71e1713
c6d50f3
9c103f6
79ee76b
9b2d520
4a820fe
8d04586
2c8e0b7
1fed8f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,3 +47,13 @@ <h4> | |
}); | ||
} | ||
</script> | ||
|
||
<script> | ||
// Lighthouse will move on to the next thing at onload, however this will race with | ||
// the cache population in the serviceworker's install phase. | ||
// We use an image that takes 2-seconds to load to delay window onload. | ||
// (Also about that document.write.. Bless me Souders for I have sinned...) | ||
document.write(` | ||
<img src="http://1.cuzillion.com/bin/resource.cgi?type=gif&sleep=2&n=1&t=${Date.now()}"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wow There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, this seems crazy, especially because we control our own server. Could you instead make an endpoint in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sg done. |
||
`); | ||
</script> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need license |
||
const http = require('http'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const parseURL = require('url').parse; | ||
|
||
function requestHandler(request, response) { | ||
const filePath = parseURL(request.url).pathname; | ||
const absoluteFilePath = path.join(__dirname, filePath); | ||
|
||
fs.exists(absoluteFilePath, fsExistsCallback); | ||
|
||
function fsExistsCallback(fileExists) { | ||
if (!fileExists) { | ||
if (!filePath.endsWith('favicon.ico')) { | ||
console.error(`Cannot find file ${absoluteFilePath}`); | ||
} | ||
return sendResponse(404, '404 - File not found'); | ||
} | ||
fs.readFile(absoluteFilePath, 'binary', readFileCallback); | ||
} | ||
|
||
function readFileCallback(err, file) { | ||
if (err) { | ||
console.error(`Unable to read local file ${absoluteFilePath}:`, err); | ||
return sendResponse(500, '500 - Internal Server Error'); | ||
} | ||
sendResponse(200, file); | ||
} | ||
|
||
function sendResponse(statusCode, data) { | ||
const headers = filePath.endsWith('.js') ? | ||
{'Content-Type': 'text/javascript'} : undefined; | ||
response.writeHead(statusCode, headers); | ||
response.write(data, 'binary'); | ||
response.end(); | ||
} | ||
} | ||
|
||
const serverForOnline = http.createServer(requestHandler); | ||
const serverForOffline = http.createServer(requestHandler); | ||
|
||
serverForOnline.on('error', e => console.error(e.code, e)); | ||
serverForOffline.on('error', e => console.error(e.code, e)); | ||
|
||
// Listen | ||
serverForOnline.listen(10200); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why the two servers? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, so they don't share a SW? That's going to make more testing like this difficult... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We just need things on different ports. If a single server could listen on multiple ports, I'd do that. But at least we can share the requestHandler. :) IMO ports and these servers are cheap and we should feel comfortable chasing #307 with this sort of setup. |
||
serverForOffline.listen(10503); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we move it to the json response so this check isn't so ridiculous? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can't as we'd have to shift this entire test to JS. another day.