Search the site
SHIPPING
dartvel build web writes static files you can upload to any shared host.
On Apache or LiteSpeed, the .htaccess it writes makes deep links and caching work.
ON THIS PAGE
Build the site
What the build writes
The .htaccess for Apache and LiteSpeed
Install it as an app, and use it offline
Fonts that download fast
Add your own HTML to every page
Other static hosts
Find text with Ctrl+F
Link to a heading
dartvel build webCopy code to clipboard
Upload the contents of build/web to your web root.
index.html
For: The app shell, with the title, description, Open Graph tags and splash
<route>/index.html
For: One page per route, so a deep link and a crawler get real HTML
manifest.json, icons/
For: The installable PWA
flutter_service_worker.js
For: Offline caching, replaced by Dartvel's worker
offline/index.html, 404/index.html
For: The not-found and offline pages, which are routes of your app
404.html
For: The not-found page again, for the hosts that look for that name
sitemap.xml, robots.txt
For: Search engines, when seo.siteUrl is set
sitemap/index.html
For: The same list as a page people can read: every page with its date, in your theme (your own /sitemap page wins)
.htaccess
For: Apache and LiteSpeed rules: routes, caching and security headers
_headers
For: The same security headers for Netlify and Cloudflare Pages
# pubspec.yaml
dartvel:
seo:
siteUrl: https://example.comCopy code to clipboard
Routes with parameters get pages for the paths your models list.
Set pwa.enabled: false to leave out the manifest and worker.
Until the app has loaded, a visitor sees the splash: your logo, a pulsing Loading line and a loading bar that follows the real downloads. They go when the app has drawn its first screen. Earlier versions showed a picture of each page instead; it was removed because it looked finished but did nothing when tapped. If your pubspec still has web.firstFrame, the build ignores it and says so: delete it.
On a slow phone network that wait can be half a minute. Turn on web.ssrFirst and each page shows its own text and links from the first moment instead of the splash, laid out as your app draws the page: the same places, fonts, colours, surfaces and images, light and dark, at phone, tablet and desktop widths. The build records what your app painted at each width and places the page text there. It is still text: readers can select it, search it, tab through it and follow its links, which open as normal pages. The loading bar stays at the top. When the app has drawn its first screen it takes over in place. Something pinned to the screen, such as a cookie banner, stays on top of the page under it, as in the app. A text field already holds the text your app starts it with. A sentence with links in it (DVWidgetSpan) is one paragraph, its links in its lines where the app draws them, so search and selection run across the links. The first thing painted is the first screen itself, in place: never a white page first, never half a page, in light and dark. A section as tall as the screen fits every screen height. A custom painter, a shader or a blur is code the build cannot read, so it appears only when the app takes over; draw glows with DVRadialGradient or DVGradientPainter and gradient text with DVGradientSpan in a DVGradientText, and the page draws those from the start. It is off unless you turn it on.
# pubspec.yaml
dartvel:
web:
ssrFirst: true # the page itself instead of the splashCopy code to clipboard
Every web build is compiled twice: Chrome, Edge and Chrome for Android run the WebAssembly version, other browsers the JavaScript one. Each page loads its own code when it is first shown. The page and the splash are the same either way.
Each page asks for its own code as soon as the app and its renderer have downloaded, instead of one piece after another once the app starts. Links on the page wait until that first screen is showing before they fetch the pages they point to, and the offline copy is made only after it too.
On the web-server build, the WebAssembly version draws on a second thread, which keeps scrolling smooth on slow phones. That needs two headers the build turns on by itself unless your site loads scripts or frames from another site, or opens sign-in or payment popups there; the build says when it leaves them off.
# pubspec.yaml
dartvel:
web:
wasm: false # JavaScript only
crossOriginIsolated: false # never send the two headersCopy code to clipboard
A Content-Security-Policy tells the browser which scripts a page may run. Your pages carry small scripts of their own (the splash and the hand-over to the app), and they change from build to build, so a policy that lists them by hand is only right for one build. Write the policy once in your pubspec with 'inline-scripts' where those scripts belong, and the web-server build fills in the right ones for every page it sends. Do not set the same header in nginx or another proxy as well: a browser enforces both, and the proxy's copy goes out of date with your next build.
# pubspec.yaml
dartvel:
security:
pageCsp: "default-src 'self'; script-src 'self' 'inline-scripts' 'wasm-unsafe-eval' https://www.gstatic.com; object-src 'none'"Copy code to clipboard
This is the file the build writes. LiteSpeed reads the same rules.
# Written by dartvel build web. Rewritten on every build while this line is
# here. To keep your own edits, delete this line: the build then leaves the
# file alone. Or put your own in the project's web/.htaccess, which the build
# copies in place of this one.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Served as themselves. Without these the fallback below swallows every
# asset and the page loads index.html as its own JavaScript.
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Everything else is a route. The application reads the URL and renders it.
RewriteRule . /index.html [L]
</IfModule>
# Where mod_rewrite is off. The rewrite above sends unknown paths to the
# application shell, which is right for a route the router knows and wrong for
# one nothing does -- and it does not run at all on a host without
# mod_rewrite, where this is what a visitor sees instead of the server's own
# branding.
ErrorDocument 404 /404/index.html
# The Universal Links document has no extension for AddType to match, and iOS
# refuses it as anything but JSON.
<Files "apple-app-site-association">
ForceType application/json
</Files>
<IfModule mod_mime.c>
# instantiateStreaming refuses a wasm module served as anything else.
AddType application/wasm .wasm
AddType application/javascript .js
AddType application/json .json
</IfModule>
<IfModule mod_headers.c>
# The headers a Dartvel server sends on every response, so a static host
# answers the same: no sniffing a file into another type, no framing by
# another site, and no full URL leaking to other origins. setifempty keeps
# one the host already sends.
Header always setifempty X-Content-Type-Options "nosniff"
Header always setifempty X-Frame-Options "SAMEORIGIN"
Header always setifempty Referrer-Policy "strict-origin-when-cross-origin"
# Not cached, because Flutter does not content-hash any of these: a build
# from today and a build from last year both produce a file called
# main.dart.js. A blanket immutable rule over *.js froze the site for every
# returning visitor -- no error, no way for them to know, and no way out
# from their side once the service worker was cached too.
<FilesMatch "^(index\.html|main\.dart\.js|flutter_bootstrap\.js|flutter_service_worker\.js|version\.json|manifest\.json)$">
Header set Cache-Control "no-cache, must-revalidate"
</FilesMatch>
# A name carrying a hash of the contents (the fonts the build writes):
# a new body is a new name. FilesMatch sees the file name alone.
<FilesMatch "\.[0-9a-f]{16}\.(woff2|ttf|otf)$">
Header set Cache-Control "public, max-age=31536000, immutable"
</FilesMatch>
# These do carry a version in their path, and they are the large ones.
<FilesMatch "^(canvaskit/|assets/).*\.(js|wasm|woff2|otf|ttf|png|jpg|svg|json)$">
Header set Cache-Control "public, max-age=31536000, immutable"
</FilesMatch>
</IfModule>Copy code to clipboard
Existing files are served as they are. Every other path loads the app, which routes it.
index.html, main.dart.js and the service worker are checked with the server on every visit, so a new upload reaches returning visitors.
Every response gets X-Content-Type-Options, X-Frame-Options (SAMEORIGIN) and a Referrer-Policy, the same headers the Dartvel server sends. A header your host already sets is kept.
Put your own web/.htaccess or web/_headers in the project and the build copies yours instead.
The build rewrites its own .htaccess every time, so fixes to the rules reach you. Delete the first line of the copy in build/web and later builds leave your edits alone.
dartvel build web writes the manifest, the icons and a service worker. A page the worker cannot reach opens /offline, a page of your app that says where you were.
Every icon comes from one image, dartvel.pwa.icon (or assets/icon.png): the tab icon (favicon.png), the home-screen icon (apple-touch-icon.png) and the four install icons. With no image the build draws a monogram of your app's name and says so. Flutter's template logo, package name, "A new Flutter project." and blue are never shipped: the name, description and colours come from dartvel.pwa, then dartvel.seo.
A change sent while offline is queued in the browser and sent in order when the network is back. CI checks this in Chrome on every push.
The install prompt reports what the visitor chose, and can be offered again after a No.
A second visit loads from the phone: the worker keeps the app, its fonts, the renderer and every page part, and the page hands it what the first visit downloaded. Pages themselves always come from the network first, so they stay fresh.
A release reaches people on their next visit. The page asks for the app at its build (main.dart.js?v=<build>), so new HTML is never paired with the old code, and only files whose contents changed are downloaded again. Every early download of the app asks for that same address, so each file comes down once.
On by default. dartvel.pwa.enabled: false turns it off; dartvel.pwa.precacheRoutes sets how many pages are saved on the first visit (25 by default, or true for all).
Built
Spec section: PWA
The engine waits for every font before it draws, so the build makes them as small as it can without changing what your pages show.
Fonts go out as WOFF2, under a name that changes with their contents, so a browser keeps them for a year.
Text fonts keep Latin (Yoruba, Igbo and Hausa letters included), common symbols and every character your own code and content use. Anything else someone types is drawn with a Noto font the engine fetches for it.
Static weights are dropped when a variable font of the same design draws them, and a font family nothing names is not downloaded.
Roboto, which Flutter otherwise fetches from fonts.gstatic.com, comes from your own site.
The build prints what it saved: fonts: 1.3 MB -> 387 KB.
# pubspec.yaml
dartvel:
web:
fonts:
characters: ['U+0400-04FF'] # also keep Cyrillic
# subset: false, useVariable: false, dropUnused: false,
# roboto: false; or fonts: false for none of itCopy code to clipboard
Analytics, a tag manager, site verification or a chat widget often comes as a snippet of HTML to paste into every page. Put it in one of three places, and every page carries it: prerendered pages, pages the web server renders, the not-found and offline pages, and Studio.
# pubspec.yaml
dartvel:
platforms:
web:
document:
head: # inside <head>
- html: <meta name="google-site-verification" content="your-code">
- file: html/analytics.html
bodyEnd: # right before </body> closes
- html: <script src="https://cdn.example.com/chat.js" async></script>Copy code to clipboard
A file entry names a file under html/, which suits a snippet longer than a line. The same setting in a Dart config class:
// The same setting in a Dart config class (`dartvel: lib/services/...` in
// pubspec.yaml). DVWebFile.analytics is generated from html/analytics.html, so
// renaming or deleting the file is a compile error.
import 'package:dartvel_core/config.dart';
import '../dartvel_client/web_files.g.dart';
class const SiteConfig() extends DartvelConfig {
@override
DVPlatformSettings get platforms => const DVPlatformSettings(
web: DVWebSettings(
document: DVWebDocumentSlots(
head: <DVHtmlSnippet>[
DVHtmlSnippet.html('<meta name="google-site-verification" content="your-code">'),
DVHtmlSnippet.file(DVWebFile.analytics),
],
bodyEnd: <DVHtmlSnippet>[
DVHtmlSnippet.html('<script src="https://cdn.example.com/chat.js" async></script>'),
],
),
),
);
}Copy code to clipboard
Three places: head (inside <head>), bodyStart (right after <body> opens) and bodyEnd (right before </body> closes, after the page itself).
Each entry is html: (the HTML itself) or file: (a file under html/; not web/, which the build generates and git ignores). In a Dart config class a file is a DVWebFile value, generated for every HTML file under html/, so a renamed file is a compile error.
Studio's Platforms screen edits the same setting. On dartvel dev it saves into pubspec.yaml; on a deployed app it waits for the GitHub screen, which sends it as a pubspec.yaml change.
The snippet goes into the page as you wrote it, and the minifier leaves it alone. It is parsed once, with the page, so it is there before the app starts and does not run again when the app takes over.
HTML that would break the page stops the build, naming the file or the entry: an element left open (a script, a comment, a div in bodyStart), a closing tag with nothing open, <html>, <head> or <body>, an element a head cannot hold, or a <title> or description, which each page sets itself.
With dartvel.security.pageCsp, the policy lists the hosts your snippets load scripts, styles, frames and images from. Inline scripts are hashed into it per page when the policy has 'inline-scripts', and from the snippet otherwise.
The HTML runs with the page's full authority. Only add HTML from a source you trust. In Studio, only the app's owner may change it, unless the app names its own editors with registerAction(dvStudioEditPlatformsAction, ...).
Web settings now live under dartvel.platforms.web. dartvel.web and dartvel.android keep working; where both homes set the same key, platforms wins. A platforms: list of targets, as dartvel create writes it, still works; Studio turns it into the map, every target kept, when it saves.
Any host works if it serves index.html for paths with no file.
dartvel deploy --target web --provider netlify runs the provider's own CLI. firebase-hosting, vercel and cloudflare work the same way.
Ctrl+F (Cmd+F on a Mac), and Find in page from a phone browser's menu, find text on a Dartvel web page. The page scrolls to the paragraph that matched and highlights it.
Flutter draws the page's words on a canvas, and the browser searches the document. So every page keeps a copy of its text in the document where the browser can search it:
The build writes each page's text into its HTML for crawlers, no-script readers and printers. Each paragraph is marked hidden="until-found", which the browser searches and does not draw.
When a match lands in a paragraph, the browser fires beforematch. Dartvel scrolls the Flutter page to that paragraph, highlights it, and hides the copy again.
After you navigate, or when the page's content changes, the copy is rewritten from what the page has drawn. Find and printing follow the page on screen.
There is nothing to add to a page. @DVPage(findable: false) keeps a page's text out of the document.
A browser match names the paragraph and not the word, so the page scrolls to the paragraph. It works in Chrome, Edge and other Chromium browsers, and in Firefox 139 and later. Safari has not been checked yet. A browser without until-found finds nothing on the page, as before.
Esc closes the find bar from the page as well as from the bar. A page answers Esc only when it has something open to close, such as a dialog, a menu or the text selection menu. Otherwise the key goes to the browser.
A list builds only the rows near the screen, so text in a row that has not been built yet is not found.
Planned
An in-app find bar in the page shell, for native builds and for browsers without until-found. Generated record tables that register the rows they have not built, so a match in a row off screen is found too. Neither is built yet.
An address ending in #heading-id opens the page at that heading, on the web and in a native app, as it does on any other web page. Flutter ignores the part after #, so a Dartvel page does this itself.
Every heading gets an id from its words: lower case, punctuation dropped, spaces as hyphens. "Find text with Ctrl+F" is #find-text-with-ctrlf.
A heading that repeats an earlier one is numbered from 2: the second "Setup" is #setup-2.
Headings are the text a page marks with semanticHeading(n). There is nothing to add to a page.
A page that loads its content later still opens at the heading: Dartvel looks for it again as the page draws.
Try it: this button goes to /docs/web-hosting#find-text-with-ctrlf.
FSL-1.1-MIT licensed. Built with Dartvel.
Dartvel is made by
To the bottom