Skip to content
This repository was archived by the owner on May 14, 2020. It is now read-only.

Commit 614c747

Browse files
committed
Supports <link rel=preload> for injected config.
1 parent 35b0dad commit 614c747

File tree

6 files changed

+84
-9
lines changed

6 files changed

+84
-9
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Server created for javascript SPA apps like: Vue, React, Angular, etc.
2323
## Docker usage
2424

2525
```Dockerfile
26-
FROM microparts/static-server-php:1.1.2
26+
FROM microparts/static-server-php:1.1.3
2727

2828
COPY dist/ /app
2929
# frontend yaml configuration
@@ -176,6 +176,8 @@ window.__vcs = '%s';
176176
console.log('%%cSTAGE=dev SHA1=55b5293; %%cSecurity bugs: [email protected], Job/partnership: [email protected]','color:#F44336','color:#009688');
177177
```
178178

179+
Also, will be injected `<link>` tag with `rel=preload`. [More](https://developers.google.com/web/tools/lighthouse/audits/preload).
180+
179181
3. Loads all content of static files to memory
180182
4. Starts the server.
181183

example/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ARG STAGE
1313
RUN npm ci --silent \
1414
&& npm run build
1515

16-
FROM microparts/static-server-php:1.1.2
16+
FROM microparts/static-server-php:1.1.3
1717

1818
COPY --from=0 /usr/app/dist /app
1919
COPY --from=0 /usr/app/configuration /app/configuration

src/Modifier/InjectConfigFileToIndexModify.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ private function toTopOfHead(DOMDocument $dom, DOMElement $script, Transfer $cha
143143
*/
144144
private function beforeFirstScript(DOMDocument $dom, DOMElement $script, Transfer $changed): Transfer
145145
{
146+
$this->configPreload($dom);
147+
146148
$scripts = $dom->getElementsByTagName('script');
147149

148150
// If can't found any <script> tag, we will skip injecting.
@@ -159,4 +161,35 @@ private function beforeFirstScript(DOMDocument $dom, DOMElement $script, Transfe
159161

160162
return $changed;
161163
}
164+
165+
/**
166+
* Preloading __config.js
167+
*
168+
* https://developers.google.com/web/tools/lighthouse/audits/preload
169+
*
170+
* @param \DOMDocument $dom
171+
*
172+
* @return void
173+
*/
174+
private function configPreload(DOMDocument $dom): void
175+
{
176+
$preload = $dom->createElement('link');
177+
$preload->setAttribute('rel', 'preload');
178+
$preload->setAttribute('href', $this->location);
179+
$preload->setAttribute('as', 'script');
180+
181+
$link = $dom->getElementsByTagName('link');
182+
183+
if ($link->length > 0) {
184+
$link->item(0)->parentNode->insertBefore($preload, $link->item(0));
185+
return;
186+
}
187+
188+
$head = $dom->getElementsByTagName('head');
189+
190+
if ($head->length > 0) {
191+
$head->item(0)->appendChild($preload);
192+
return;
193+
}
194+
}
162195
}

tests/Modifier/InjectConfigToIndexHandlerTest.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ public function testHowInjectingSkipFilesExceptIndex()
2727
public function testHowInjectingWorksWithStandardCase()
2828
{
2929
$results = $this->newInjectHandle('tests_inject_head', '/vue/index.html');
30-
$this->assertInject($results);
30+
$this->assertStringContainsString('/__config.js', $results->getContent());
31+
$this->assertStringContainsString('preload', $results->getContent());
3132
}
3233

3334
public function testHowInjectingWorksWithoutHeadSection()
3435
{
3536
$results = $this->newInjectHandle('tests_inject_head');
36-
$this->assertInject($results);
37+
$this->assertStringContainsString('/__config.js', $results->getContent());
3738
}
3839

3940
public function testHowInjectingWorksWithInvalidValue()
@@ -46,12 +47,23 @@ public function testHowInjectingWorksWithEmptyHeadSection()
4647
{
4748
$results = $this->newInjectHandle('tests_inject_head', '/empty_index/index.html');
4849
$this->assertStringNotContainsString('__config', $results->getContent());
50+
$this->assertStringNotContainsString('preload', $results->getContent());
4951
}
5052

5153
public function testHowInjectingWorksWithoutScriptTag()
5254
{
5355
$results = $this->newInjectHandle('tests', '/empty_index/index.html');
5456
$this->assertStringNotContainsString('__config', $results->getContent());
57+
$this->assertStringNotContainsString('preload', $results->getContent());
58+
}
59+
60+
public function testHowInjectingPreloading()
61+
{
62+
$results = $this->newInjectHandle('tests', '/head_link_exists/index.html');
63+
$this->assertStringContainsString('preload', $results->getContent());
64+
65+
$results = $this->newInjectHandle('tests', '/head_link_not_exists/index.html');
66+
$this->assertStringContainsString('preload', $results->getContent());
5567
}
5668

5769
/**
@@ -72,9 +84,4 @@ private function newInjectHandle(string $config, string $location = '/empty_head
7284

7385
return $handler($transfer, $transfer);
7486
}
75-
76-
private function assertInject(Transfer $transfer)
77-
{
78-
$this->assertStringContainsString('/__config.js', $transfer->getContent());
79-
}
8087
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
6+
name="viewport">
7+
<meta content="ie=edge" http-equiv="X-UA-Compatible">
8+
<title>Document</title>
9+
<link rel="stylesheet" href="">
10+
</head>
11+
<body>
12+
hello stranger!
13+
<script>
14+
15+
</script>
16+
</body>
17+
</html>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
6+
name="viewport">
7+
<meta content="ie=edge" http-equiv="X-UA-Compatible">
8+
<title>Document</title>
9+
</head>
10+
<body>
11+
hello stranger!
12+
<script>
13+
14+
</script>
15+
</body>
16+
</html>

0 commit comments

Comments
 (0)