PHP code example of mxent / pwax
1. Go to this page and download the library: Download mxent/pwax library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mxent / pwax example snippets
Route::get('/', fn () => pwaxRender('pages.home', [
'greeting' => 'Hello, ' . auth()->user()?->name,
]))->name('index');
// routes/web.php
Route::get('/', fn () => pwaxRender('pages.home'))->name('index');
Route::get('/posts/{post}', fn (Post $post) => pwaxRender('pages.post', [
'post' => $post,
'canEdit' => auth()->user()?->can('update', $post),
]))->name('posts.show');
Route::get('/', fn () => pwaxRender('pages.home'))->name('index');
Route::get('/about', fn () => pwaxRender('pages.about'))->name('about');
Route::middleware('auth')->group(function () {
Route::get('/settings', fn () => pwaxRender('pages.settings'))->name('settings');
});
Route::middleware('auth')->get('/settings', fn () => pwaxRender('pages.settings'));
'progress' => [
'enabled' => true,
'color' => null, // defaults to customization.init_spinner_color
'height' => 3,
'delay' => 250,
'trickle' => true,
],
'transition' => [
'duration' => 150, // cross-fade length in milliseconds
],
'vue' => [
'plugins' => [
// A Pwax component whose default export is a Vue plugin.
'toast' => "@pwaxImport('plugins.toast')",
// A UMD library already loaded by a <script> tag.
'i18n' => 'VueI18n.createI18n',
],
'directives' => [
'focus' => "@pwaxImport('directives.focus')",
],
'middleware' => [
'confirmed' => "@pwaxImport('middleware.confirmed')",
],
],
'manifest' => [
// Set this once and never change it. Without it a browser identifies the installed
// app by start_url, so changing start_url later orphans every existing install.
'id' => '/',
'name' => 'My Application',
'short_name' => 'MyApp',
'theme_color' => '#0c83ff',
'icons' => [
['src' => '/images/icon-192.png', 'sizes' => '192x192', 'type' => 'image/png'],
['src' => '/images/icon-512.png', 'sizes' => '512x512', 'type' => 'image/png'],
['src' => '/images/maskable.png', 'sizes' => '512x512', 'type' => 'image/png',
'purpose' => 'maskable'],
],
'screenshots' => [
['src' => '/images/wide.png', 'sizes' => '1280x720', 'type' => 'image/png',
'form_factor' => 'wide'],
],
],
'manifest' => [
// Opened when the user picks your app from the share sheet.
'share_target' => [
'action' => '/share',
'method' => 'POST',
'enctype' => 'multipart/form-data',
'params' => ['title' => 'title', 'text' => 'text', 'url' => 'url'],
],
// Opened when the user opens a file your app claims.
'file_handlers' => [
['action' => '/import', 'accept' => ['text/csv' => ['.csv']]],
],
// Opened when something follows a web+invoice: link.
'protocol_handlers' => [
['protocol' => 'web+invoice', 'url' => '/invoices/open?ref=%s'],
],
],
'service_worker' => ['enabled' => true],
'service_worker' => [
'enabled' => true,
'components' => 'all', // every component (default)
'components' => ['components.*', 'ui.*'], // only these
'components' => false, // none; cached lazily as they load
'exclude' => ['vendor.pwax.*', 'admin.*'],
],
'service_worker' => [
'asset_groups' => [
[
'name' => 'app',
'install_mode' => 'prefetch', // fetched at install
'files' => ['/favicon.ico', '/css/**.css', '/js/**.js', '/build/**'],
],
[
'name' => 'assets',
'install_mode' => 'lazy', // fetched on first use, then kept
'files' => ['/images/**', '/fonts/**'],
],
],
],
'service_worker' => [
'data_groups' => [
[
'name' => 'posts',
'urls' => ['/api/posts', '/api/posts/**'],
'strategy' => 'network-first', // or 'cache-first' to serve the cache first
'max_entries' => 50,
'max_age' => 3600, // seconds, for 'cache-first'
'timeout' => 3000, // ms before 'network-first' falls back to the cache
],
],
],
'service_worker' => ['namespaces' => ['acme-ui']], // finds @pwaxImport('acme-ui::button')
'service_worker' => ['version' => 'v2'],
Route::get('/settings', fn () => pwaxRender('pages.settings')); // found
'service_worker' => ['pages' => ['urls' => ['/posts/hello-world']]],
Route::get('/docs/{page}', fn ($page) => pwaxRender('pages.docs', [...])
->cacheable(86400, shared: true));
'service_worker' => ['pages' => ['runtime' => true]],
Route::get('/recovery-codes', fn () => pwaxRender('pages.codes')->offline(false));
'push' => [
'public_key' => env('VAPID_PUBLIC_KEY'),
'private_key' => env('VAPID_PRIVATE_KEY'),
'endpoint' => '/push/subscriptions',
'title' => config('app.name'), // used when a message sends no title
'icon' => '/images/icons/icon-192.png',
'badge' => '/images/icons/badge-72.png',
],
Route::post('/push/subscriptions', function (Request $request) {
$data = $request->validate([
'endpoint' => ['er()->pushSubscriptions()->updateOrCreate(
['endpoint' => $data['endpoint']],
['p256dh' => $data['keys']['p256dh'], 'auth' => $data['keys']['auth']],
);
return response()->noContent();
})->middleware('auth');
Route::delete('/push/subscriptions', function (Request $request) {
$request->user()->pushSubscriptions()
->where('endpoint', $request->input('endpoint'))
->delete();
return response()->noContent();
})->middleware('auth');
'service_worker' => [
'extend' => ['js.analytics-sync'],
],
'service_worker' => [
'blade' => 'js.my-worker',
],
'assets' => ['source' => 'cdn'],
'assets' => ['pinia' => false],
'minify' => ['enabled' => false],
'assets' => ['vue_build' => 'runtime'],
Route::get('/posts/{post}', fn (Post $post) => pwaxRender('pages.post', compact('post'))
->title($post->title)
->description($post->excerpt)
->canonical(route('posts.show', $post))
->image($post->cover_url)
->robots($post->draft ? 'noindex' : 'index, follow')
->alternate('fr', route('posts.show', [$post, 'locale' => 'fr']))
->jsonLd([
'@context' => 'https://schema.org',
'@type' => 'Article',
'headline' => $post->title,
'datePublished' => $post->published_at->toIso8601String(),
]));
->meta(['author' => 'Ada Lovelace', 'rating' => 'general'])
'head' => [
'title' => null, // falls back to manifest.name
'title_template' => ':title · Acme',
'description' => null, // falls back to manifest.description
'image' => '/img/og.png', // the sharing card for pages that name none
'robots' => null, // 'noindex, nofollow' on staging
'locale' => null, // og:locale; defaults to the app locale
'alternates' => [], // ['en' => '/', 'fr' => '/fr']
'json_ld' => null, // usually the site's own Organization
'open_graph' => true,
'open_graph_type' => 'website',
'twitter_card' => null, // follows the image
],
->jsonLd(['@context' => 'https://schema.org', '@type' => 'Article', 'headline' => $post->title])
->jsonLd(['@context' => 'https://schema.org', '@type' => 'BreadcrumbList', 'itemListElement' => $crumbs])
->alternate('en', route('posts.show', $post))
->alternate('fr', route('posts.show', [$post, 'locale' => 'fr']))
->alternate('x-default', route('posts.show', $post))
'components' => ['allowed' => ['pages.*', 'components.*']],
'csp' => ['nonce' => fn () => request()->attributes->get('csp-nonce')],
'security' => [
'cross_origin_opener_policy' => 'same-origin',
'cross_origin_embedder_policy' => '
'service_worker' => ['pages' => ['strategy' => 'cache-first']],
bash
php artisan pwax:component pages.home
bash
php artisan pwax:doctor
blade
{{-- resources/views/middleware/confirmed.blade.php --}}
<script>
export default async function ({ component, meta, redirect }) {
if (meta.js
window.pwax.launch.consume(({ files, targetURL }) => {
for (const handle of files) {
readAndImport(handle); // FileSystemFileHandle
}
});
bash
php artisan pwax:precache # what will be available offline
php artisan pwax:precache --verify # …and which components actually render
php artisan pwax:precache --json # the manifest itself
bash
> PHP_CLI_SERVER_WORKERS=8 php artisan serve
> bash
php artisan vendor:publish --tag=pwax-service-worker
bash
php artisan pwax:push-endpoint
bash
php artisan pwax:vapid
blade
{{-- resources/views/js/analytics-sync.blade.php --}}
self.addEventListener('periodicsync', (event) => {
if (event.tag === 'refresh-feed') {
event.waitUntil(fetch('/feed/refresh'));
}
});
bash
php artisan pwax:compile