Skip to content

Commit af9ebb2

Browse files
Handle Cloudflare Rocket Loader more gracefully
1 parent c602f76 commit af9ebb2

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

app/FlarumVersionGuesser.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ class FlarumVersionGuesser
1313
public function guess(string $html, string $bootScript): array
1414
{
1515
// Separate JSON payload tag was introduced in 1.4 https://github.com/flarum/framework/pull/3461
16-
if (preg_match('~<script\s+id="flarum-json-payload"\s+type="application/json"~', $html) === 1) {
16+
// Cloudflare Rocket Loader removes the quotes around the ID
17+
if (preg_match('~<script\s+id=("flarum-json-payload"|flarum-json-payload)\s+type="application/json"~', $html) === 1) {
1718
return [
1819
FlarumVersion::V1_4_0,
1920
FlarumVersion::V1_5_0,
@@ -37,7 +38,8 @@ public function guess(string $html, string $bootScript): array
3738
}
3839

3940
// Hashes changed in Flarum 1.0 https://github.com/flarum/core/pull/2805
40-
if (preg_match('~/assets/forum\.js\?v=[0-9a-f]{8}"></script>~', $html) === 1) {
41+
// Cloudflare Rocket Loader adds a type="" attribute to all script tags
42+
if (preg_match('~/assets/forum\.js\?v=[0-9a-f]{8}"\s*(type="[^"]+")?></script>~', $html) === 1) {
4143
return [
4244
FlarumVersion::V1_0_0,
4345
FlarumVersion::V1_0_1,

app/Jobs/ScanHomePage.php

+8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ protected function handleTask()
4444
$versions = null;
4545

4646
$homepage->filter('body script')->each(function (Crawler $script) use ($bodyContent, &$bootPayload, &$versions) {
47+
// Cloudflare's Rocket Loader complicates the parsing of known tags
48+
// We'll add a warning about so the user is aware why the scan might be incomplete
49+
if (str_contains($script->attr('src'), 'cloudflare-static/rocket-loader')) {
50+
$this->data['cloudflareRocketLoader'] = true;
51+
52+
return;
53+
}
54+
4755
$content = $script->text('', false);
4856

4957
// Starting with Flarum 1.4, the boot payload is in its own tag

resources/assets/js/pages/ScanPage.js

+14
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,20 @@ export default {
224224
return getObjectKey(task.attributes.data, key, defaultValue);
225225
}
226226

227+
if (reportKey('ScanHomePage', 'cloudflareRocketLoader')) {
228+
suggestions.push({
229+
title: 'Rocket Loader',
230+
suggest: [
231+
'This website appears to be enhanced with ',
232+
m('a', {
233+
href: 'https://developers.cloudflare.com/fundamentals/speed/rocket-loader/',
234+
}, 'Cloudflare Rocket Loader'),
235+
'. Unfortunately not all features of the Lab work with Rocket Loader at the moment. ',
236+
'You might see incomplete results.',
237+
],
238+
});
239+
}
240+
227241
// We could probably switch to ScanGuessVersion but if it ain't broken...
228242
const legacyVersions = reportKey('ScanHomePage', 'versions', []);
229243

0 commit comments

Comments
 (0)