Skip to content

Commit

Permalink
remove inline scripts as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Jun 11, 2020
1 parent 9f6cec5 commit 86c39e6
Show file tree
Hide file tree
Showing 25 changed files with 105 additions and 82 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -27,3 +27,4 @@ storage/options.php
.php_cs.cache
resources/views/overrides
public/sw.js
public/meta.js
17 changes: 16 additions & 1 deletion app/Http/View/Composers/HeadComposer.php
Expand Up @@ -43,6 +43,7 @@ public function compose(View $view)
$this->seo($view);
$this->injectStyles($view);
$this->addExtra($view);
$this->serializeGlobals($view);
}

public function addFavicon(View $view)
Expand Down Expand Up @@ -112,7 +113,7 @@ public function injectStyles(View $view)
'integrity' => 'sha256-h20CPZ0QyXlBuAw7A+KluUYx/3pK+c7lYEpqLTlxjYQ=',
'crossorigin' => 'anonymous',
];
if (!$this->request->is('/')) {
if (!$this->request->is('/') && config('app.asset.env') !== 'development') {
$links[] = [
'rel' => 'stylesheet',
'href' => $this->webpack->url('style.css'),
Expand All @@ -131,4 +132,18 @@ public function addExtra(View $view)
$this->dispatcher->dispatch(new \App\Events\RenderingHeader($content));
$view->with('extra_head', $content);
}

public function serializeGlobals(View $view)
{
$blessing = [
'version' => config('app.version'),
'locale' => config('app.locale'),
'base_url' => url('/'),
'site_name' => option_localized('site_name'),
'route' => request()->path(),
'i18n' => new \stdClass(),
'extra' => [],
];
$view->with('blessing', $blessing);
}
}
22 changes: 0 additions & 22 deletions app/Listeners/SerializeGlobals.php

This file was deleted.

3 changes: 0 additions & 3 deletions app/Providers/EventServiceProvider.php
Expand Up @@ -20,9 +20,6 @@ class EventServiceProvider extends ServiceProvider
'App\Events\PluginBootFailed' => [
Listeners\NotifyFailedPlugin::class,
],
'App\Events\RenderingHeader' => [
Listeners\SerializeGlobals::class,
],
'auth.registration.completed' => [
Listeners\SendEmailVerification::class,
],
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -11,7 +11,7 @@
"private": true,
"scripts": {
"dev": "webpack-dev-server",
"build": "webpack --mode=production -p --progress",
"build": "webpack -p --progress && webpack -p --config=webpack.meta.config.js",
"lint": "eslint --ext=ts -f=beauty .",
"fmt": "prettier --write resources/assets",
"test": "jest",
Expand Down Expand Up @@ -198,6 +198,7 @@
"coveragePathIgnorePatterns": [
"/node_modules/",
"<rootDir>/resources/assets/src/styles",
"<rootDir>/resources/assets/src/scripts/extra.ts",
"<rootDir>/resources/assets/src/scripts/urls.ts",
"<rootDir>/resources/assets/tests/setup",
"<rootDir>/resources/assets/tests/utils",
Expand Down
1 change: 1 addition & 0 deletions resources/assets/src/scripts/app.ts
@@ -1,4 +1,5 @@
import './init' // Must be first
import './extra'
import './i18n'
import './net'
import './event'
Expand Down
12 changes: 12 additions & 0 deletions resources/assets/src/scripts/extra.ts
@@ -0,0 +1,12 @@
export function getExtraData(): any {
const jsonElement = document.querySelector('#blessing-extra')
/* istanbul ignore next */
if (jsonElement) {
return JSON.parse(jsonElement.textContent ?? '{}')
}
}

const extraData = getExtraData()
if (extraData) {
blessing.extra = extraData
}
8 changes: 6 additions & 2 deletions resources/assets/src/scripts/home-page.ts
@@ -1,4 +1,5 @@
import '@/styles/home.css'
import { getExtraData } from './extra'

export function scrollHander() {
const header = document.querySelector('.navbar')
Expand Down Expand Up @@ -28,8 +29,11 @@ export async function logout() {
}

/* istanbul ignore next */
if (blessing.extra.transparent_navbar) {
window.addEventListener('load', scrollHander)
if (process.env.NODE_ENV !== 'test') {
const { transparent_navbar }: { transparent_navbar: boolean } = getExtraData()
if (transparent_navbar) {
window.addEventListener('load', scrollHander)
}
}
/* istanbul ignore next */
document
Expand Down
9 changes: 9 additions & 0 deletions resources/assets/src/scripts/meta.ts
@@ -0,0 +1,9 @@
{
const blessingElement = document.querySelector('#blessing-globals')!
// @ts-ignore
window.blessing = JSON.parse(blessingElement.textContent!)

window.addEventListener('load', () => {
navigator.serviceWorker.register('/sw.js?v4')
})
}
4 changes: 2 additions & 2 deletions resources/views/admin/customize.twig
Expand Up @@ -71,7 +71,7 @@
{% endblock %}

{% block before_foot %}
<script>
blessing.extra = {{ extra|json_encode|raw }}
<script id="blessing-extra" type="application/json">
{{ extra|json_encode|raw }}
</script>
{% endblock %}
4 changes: 2 additions & 2 deletions resources/views/admin/users.twig
Expand Up @@ -4,7 +4,7 @@

{% block before_foot %}
{% set extra = {'currentUser': auth_user()} %}
<script>
blessing.extra = {{ extra|json_encode|raw }}
<script id="blessing-extra" type="application/json">
{{ extra|json_encode|raw }}
</script>
{% endblock %}
7 changes: 2 additions & 5 deletions resources/views/auth/forgot.twig
Expand Up @@ -13,10 +13,7 @@
{% endblock %}

{% block before_foot %}
<script>
Object.defineProperty(blessing, 'extra', {
configurable: false,
get: () => Object.freeze({{ extra|json_encode|raw }})
})
<script id="blessing-extra" type="application/json">
{{ extra|json_encode|raw }}
</script>
{% endblock %}
7 changes: 2 additions & 5 deletions resources/views/auth/login.twig
Expand Up @@ -9,10 +9,7 @@
{% endblock %}

{% block before_foot %}
<script>
Object.defineProperty(blessing, 'extra', {
configurable: false,
get: () => Object.freeze({{ extra|json_encode|raw }})
})
<script id="blessing-extra" type="application/json">
{{ extra|json_encode|raw }}
</script>
{% endblock %}
7 changes: 2 additions & 5 deletions resources/views/auth/register.twig
Expand Up @@ -9,10 +9,7 @@
{% endblock %}

{% block before_foot %}
<script>
Object.defineProperty(blessing, 'extra', {
configurable: false,
get: () => Object.freeze({{ extra|json_encode|raw }})
})
<script id="blessing-extra" type="application/json">
{{ extra|json_encode|raw }}
</script>
{% endblock %}
4 changes: 2 additions & 2 deletions resources/views/home.twig
Expand Up @@ -158,8 +158,8 @@
{% if home_page_css_loader %}
<script src="{{ home_page_css_loader }}"></script>
{% endif %}
<script>
blessing.extra = { transparent_navbar: {{ transparent_navbar ? 'true' : 'false' }} }
<script type="application/json" id="blessing-extra">
{{ {transparent_navbar: transparent_navbar}|json_encode|raw }}
</script>
{{ include('shared.foot') }}
</body>
Expand Down
7 changes: 2 additions & 5 deletions resources/views/shared/head.twig
Expand Up @@ -10,11 +10,8 @@
{% if custom_cdn_host %}
<link rel="preconnect" href="{{ custom_cdn_host }}" crossorigin>
{% endif %}
<script>
window.addEventListener('load', () => {
navigator.serviceWorker.register('/sw.js?v4')
})
</script>
<script id="blessing-globals" type="application/json">{{ blessing|json_encode|raw }}</script>
<script src="{{ url('/meta.js') }}"></script>
{% for link in links %}
<link{% for attribute, value in link %} {{ attribute }}="{{ value|raw }}"{% endfor %}>
{% endfor %}
Expand Down
4 changes: 2 additions & 2 deletions resources/views/skinlib/index.twig
Expand Up @@ -7,7 +7,7 @@
{% endblock %}

{% block before_foot %}
<script>
blessing.extra = {{ {currentUid: auth_user().uid}|json_encode|raw }}
<script id="blessing-extra" type="application/json">
{{ {currentUid: auth_user().uid}|json_encode|raw }}
</script>
{% endblock %}
9 changes: 3 additions & 6 deletions resources/views/skinlib/show.twig
Expand Up @@ -30,10 +30,7 @@
{% endblock %}

{% block before_foot %}
<script>
Object.defineProperty(blessing, 'extra', {
configurable: false,
get: () => Object.freeze({{ extra|json_encode|raw }})
})
</script>
<script id="blessing-extra" type="application/json">
{{ extra|json_encode|raw }}
</script>
{% endblock %}
4 changes: 2 additions & 2 deletions resources/views/skinlib/upload.twig
Expand Up @@ -27,7 +27,7 @@
{% endblock %}

{% block before_foot %}
<script>
blessing.extra = {{ extra|json_encode|raw }}
<script id="blessing-extra" type="application/json">
{{ extra|json_encode|raw }}
</script>
{% endblock %}
6 changes: 2 additions & 4 deletions resources/views/user/closet.twig
Expand Up @@ -9,9 +9,7 @@
{% endblock %}

{% block before_foot %}
<script>
Object.defineProperty(blessing, 'extra', {
get: () => Object.freeze({{ extra|json_encode|raw }})
})
<script id="blessing-extra" type="application/json">
{{ extra|json_encode|raw }}
</script>
{% endblock %}
6 changes: 2 additions & 4 deletions resources/views/user/index.twig
Expand Up @@ -36,9 +36,7 @@
{% endblock %}

{% block before_foot %}
<script>
Object.defineProperty(blessing, 'extra', {
get: () => Object.freeze({{ extra|json_encode|raw }})
})
<script id="blessing-extra" type="application/json">
{{ extra|json_encode|raw }}
</script>
{% endblock %}
4 changes: 3 additions & 1 deletion resources/views/user/player.twig
Expand Up @@ -9,5 +9,7 @@
{% endblock %}

{% block before_foot %}
<script>blessing.extra = {{ extra|json_encode|raw }}</script>
<script id="blessing-extra" type="application/json">
{{ extra|json_encode|raw }}
</script>
{% endblock %}
1 change: 0 additions & 1 deletion scripts/build.ps1
Expand Up @@ -12,7 +12,6 @@ if (Test-Path ./public/app) {

# Run webpack
yarn build
Move-Item -Path ./public/app/sw.js -Destination ./public -Force

if ($Simple) {
exit
Expand Down
8 changes: 1 addition & 7 deletions webpack.config.js
Expand Up @@ -11,7 +11,6 @@ const config = {
mode: devMode ? 'development' : 'production',
entry: {
app: ['react-hot-loader/patch', '@/index.tsx'],
sw: '@/scripts/sw.ts',
style: ['@/styles/common.css'],
home: '@/scripts/home-page.ts',
spectre: [
Expand All @@ -22,12 +21,7 @@ const config = {
},
output: {
path: `${__dirname}/public/app`,
filename: ({ chunk }) =>
chunk.name === 'sw'
? 'sw.js'
: devMode
? '[name].js'
: '[name].[contenthash:7].js',
filename: devMode ? '[name].js' : '[name].[contenthash:7].js',
chunkFilename: devMode ? '[id].js' : '[id].[contenthash:7].js',
},
module: {
Expand Down
29 changes: 29 additions & 0 deletions webpack.meta.config.js
@@ -0,0 +1,29 @@
const devMode = !process.argv.includes('-p')

module.exports = {
mode: devMode ? 'development' : 'production',
entry: {
meta: './resources/assets/src/scripts/meta.ts',
sw: './resources/assets/src/scripts/sw.ts',
},
output: {
path: `${__dirname}/public`,
filename: '[name].js',
},
module: {
rules: [
{
test: /\.ts$/,
loader: 'ts-loader',
options: {
configFile: 'tsconfig.build.json',
transpileOnly: true,
},
},
],
},
resolve: {
extensions: ['.js', '.ts', '.tsx', '.json'],
},
stats: 'errors-only',
}

0 comments on commit 86c39e6

Please sign in to comment.