forked from google/webpackager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoc.go
63 lines (55 loc) · 2.63 KB
/
doc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
Package webpackager implements the control flow of Web Packager.
The code below illustrates the usage of this package:
packager := webpackager.NewPackager(webpackager.Config{
ExchangeFactory: &exchange.Factory{
Version: version.Version1b3,
MIRecordSize: 4096,
// ... (you need to set other fields)
},
ResourceCache: filewrite.NewFileWriteCache(&filewrite.Config{
BaseCache: cache.NewOnMemoryCache(),
ExchangeMapping: filewrite.AddBaseDir(
filewrite.AppendExt(filewrite.UsePhysicalURLPath(), *flagSXGExt), *flagSXGDir),
}),
})
for _, url := range urls {
if err := packager.Run(url); err != nil {
log.Print(err)
}
}
Config allows you to change some behaviors of the Packager. packager.Run(url)
retrieves an HTTP response using FetchClient, processes it using Processor,
and turns it into a signed exchange using ExchangeFactory. Processor inspects
the HTTP response to see the eligibility for signed exchanges and manipulates
it to optimize the page loading. The generated signed exchanges are stored in
ResourceCache to prevent duplicates.
The code above sets just two parameters, ExchangeFactory and ResourceCache,
and uses the defaults for other parameters. With this setup, the packager
retrieves the content just through HTTP, applies the recommended set of
optimizations, generates signed exchanges compliant with the version b3, and
saves them in files named like "index.html.sxg" under "/tmp/sxg".
Config has a few more parameters. See its definition for the details.
You can also pass your own implementations to Config to inject custom logic
into the packaging flow. You could write, for example, a custom FetchClient
to retrieve the content from a database table instead of web servers, a custom
Processor or HTMLTask to apply your optimization techniques, a ResourceCache
to store the produced signed exchanges into another database table in addition
to a local drive, and so on.
The cmd/webpackager package provides a command line interface to execute the
packaging flow without writing the driver code on your own.
*/
package webpackager