afarkas/html5shiv
HTML5 Shiv enables HTML5 elements and basic styling support in older versions of Internet Explorer by creating missing elements and adding needed shims. A lightweight drop-in script for legacy browser compatibility with modern HTML markup.
Installation:
composer require afarkas/html5shiv
Add the script to your resources/views/layouts/app.blade.php (or equivalent) via:
<script src="{{ asset('vendor/afarkas/html5shiv/dist/html5shiv.min.js') }}"></script>
Basic Usage:
Place the script before your closing </body> tag. No additional configuration is needed for basic HTML5 shiv functionality.
First Use Case:
Test with legacy IE (8-9) by using HTML5 semantic elements (<header>, <section>, <footer>, etc.) in your Blade templates. Verify rendering via browser dev tools or tools like BrowserStack.
Conditional Loading:
Use Laravel's @stack directives to load the script only for legacy browsers:
@if (Request::isMobile() || Request::userAgent() === 'MSIE')
<script src="{{ asset('vendor/afarkas/html5shiv/dist/html5shiv.min.js') }}"></script>
@endif
Tip: Use a package like jenssegers/agent for robust user-agent detection.
Asset Optimization: Include the script in your Laravel Mix/Webpack build for minification/bundling:
// webpack.mix.js
mix.js('resources/js/app.js', 'public/js')
.copy('node_modules/afarkas/html5shiv/dist/html5shiv.min.js', 'public/vendor/html5shiv/');
Blade Directives: Create a custom Blade directive for reusable script injection:
// AppServiceProvider.php
Blade::directive('html5shiv', function () {
return '<script src="{{ asset("vendor/html5shiv/html5shiv.min.js") }}"></script>';
});
Usage:
@html5shiv
classList.js) for broader IE support.html5shiv is loaded before Modernizr’s script.mix.copy() without hashing).IE-Specific Quirks:
display: block for <header>).shiv for older document modes (force IE9 mode via <meta http-equiv="X-UA-Compatible" content="IE=9">).Asset Paths:
/vendor/...) breaks when deploying to subdirectories. Use Laravel’s asset() helper or environment-aware paths.Deprecation:
<script src="//cdn.jsdelivr.net/html5shiv/3.7.3/html5shiv.min.js"></script>
SCRIPT1028 (cross-domain issues) if loading via CDN. Use absolute URLs or local paths.Custom Builds:
Modify the source (e.g., html5shiv.js) to include/exclude specific elements or add debug logs.
Server-Side Detection: Use Laravel middleware to inject the script dynamically:
// app/Http/Middleware/InjectHtml5Shiv.php
public function handle($request, Closure $next) {
if ($request->isLegacyBrowser()) {
$response = $next($request);
$response->getContent();
$response->setContent(
str_replace('</body>', '<script src="/vendor/html5shiv/html5shiv.min.js"></script></body>', $response->getContent())
);
return $response;
}
return $next($request);
}
Fallback for Critical Path: Inline the script in Blade for above-the-fold content:
@if (Request::isLegacyBrowser())
<script>
document.createElement('section');
// ... (full html5shiv code)
</script>
@endif
How can I help you explore Laravel packages today?