mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-17 20:52:14 +01:00
feat(chromium): skip networkIdle event
This commit is contained in:
@@ -259,7 +259,7 @@ func (b *chromiumBrowser) pdf(ctx context.Context, logger *zap.Logger, url, outp
|
|||||||
runtime.Enable(),
|
runtime.Enable(),
|
||||||
disableJavaScriptActionFunc(logger, b.arguments.disableJavaScript),
|
disableJavaScriptActionFunc(logger, b.arguments.disableJavaScript),
|
||||||
extraHttpHeadersActionFunc(logger, options.ExtraHttpHeaders),
|
extraHttpHeadersActionFunc(logger, options.ExtraHttpHeaders),
|
||||||
navigateActionFunc(logger, url),
|
navigateActionFunc(logger, url, options.SkipNetworkIdleEvent),
|
||||||
hideDefaultWhiteBackgroundActionFunc(logger, options.OmitBackground, options.PrintBackground),
|
hideDefaultWhiteBackgroundActionFunc(logger, options.OmitBackground, options.PrintBackground),
|
||||||
forceExactColorsActionFunc(),
|
forceExactColorsActionFunc(),
|
||||||
emulateMediaTypeActionFunc(logger, options.EmulatedMediaType),
|
emulateMediaTypeActionFunc(logger, options.EmulatedMediaType),
|
||||||
|
|||||||
@@ -380,6 +380,41 @@ func TestChromiumBrowser_pdf(t *testing.T) {
|
|||||||
"'file:///etc/passwd' matches the expression from the denied list",
|
"'file:///etc/passwd' matches the expression from the denied list",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
scenario: "skip networkIdle event",
|
||||||
|
browser: newChromiumBrowser(
|
||||||
|
browserArguments{
|
||||||
|
binPath: os.Getenv("CHROMIUM_BIN_PATH"),
|
||||||
|
wsUrlReadTimeout: 5 * time.Second,
|
||||||
|
allowList: regexp.MustCompile(""),
|
||||||
|
denyList: regexp.MustCompile(""),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
fs: func() *gotenberg.FileSystem {
|
||||||
|
fs := gotenberg.NewFileSystem()
|
||||||
|
|
||||||
|
err := os.MkdirAll(fs.WorkingDirPath(), 0o755)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf(fmt.Sprintf("expected no error but got: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.WriteFile(fmt.Sprintf("%s/index.html", fs.WorkingDirPath()), []byte("<h1>Skip networkIdle event</h1>"), 0o755)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("expected no error but got: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fs
|
||||||
|
}(),
|
||||||
|
options: Options{
|
||||||
|
SkipNetworkIdleEvent: true,
|
||||||
|
},
|
||||||
|
noDeadline: false,
|
||||||
|
start: true,
|
||||||
|
expectError: false,
|
||||||
|
expectedLogEntries: []string{
|
||||||
|
"skipping network idle event",
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
scenario: "ErrConsoleExceptions",
|
scenario: "ErrConsoleExceptions",
|
||||||
browser: newChromiumBrowser(
|
browser: newChromiumBrowser(
|
||||||
|
|||||||
@@ -69,6 +69,13 @@ type Chromium struct {
|
|||||||
|
|
||||||
// Options are the available expectedOptions for converting HTML document to PDF.
|
// Options are the available expectedOptions for converting HTML document to PDF.
|
||||||
type Options struct {
|
type Options struct {
|
||||||
|
// SkipNetworkIdleEvent set if the conversion should wait for the
|
||||||
|
// "networkIdle" event, drastically improving the conversion speed. It may
|
||||||
|
// not be suitable for all HTML documents, as some may not be fully
|
||||||
|
// rendered until this event is fired.
|
||||||
|
// Optional.
|
||||||
|
SkipNetworkIdleEvent bool
|
||||||
|
|
||||||
// FailOnConsoleExceptions sets if the conversion should fail if there are
|
// FailOnConsoleExceptions sets if the conversion should fail if there are
|
||||||
// exceptions in the Chromium console.
|
// exceptions in the Chromium console.
|
||||||
// Optional.
|
// Optional.
|
||||||
@@ -171,6 +178,7 @@ type Options struct {
|
|||||||
// DefaultOptions returns the default values for Options.
|
// DefaultOptions returns the default values for Options.
|
||||||
func DefaultOptions() Options {
|
func DefaultOptions() Options {
|
||||||
return Options{
|
return Options{
|
||||||
|
SkipNetworkIdleEvent: false,
|
||||||
FailOnConsoleExceptions: false,
|
FailOnConsoleExceptions: false,
|
||||||
WaitDelay: 0,
|
WaitDelay: 0,
|
||||||
WaitWindowStatus: "",
|
WaitWindowStatus: "",
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ func FormDataChromiumPdfOptions(ctx *api.Context) (*api.FormData, Options) {
|
|||||||
defaultOptions := DefaultOptions()
|
defaultOptions := DefaultOptions()
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
skipNetworkIdleEvent bool
|
||||||
failOnConsoleExceptions bool
|
failOnConsoleExceptions bool
|
||||||
waitDelay time.Duration
|
waitDelay time.Duration
|
||||||
waitWindowStatus string
|
waitWindowStatus string
|
||||||
@@ -42,6 +43,7 @@ func FormDataChromiumPdfOptions(ctx *api.Context) (*api.FormData, Options) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
form := ctx.FormData().
|
form := ctx.FormData().
|
||||||
|
Bool("skipNetworkIdleEvent", &skipNetworkIdleEvent, defaultOptions.SkipNetworkIdleEvent).
|
||||||
Bool("failOnConsoleExceptions", &failOnConsoleExceptions, defaultOptions.FailOnConsoleExceptions).
|
Bool("failOnConsoleExceptions", &failOnConsoleExceptions, defaultOptions.FailOnConsoleExceptions).
|
||||||
Duration("waitDelay", &waitDelay, defaultOptions.WaitDelay).
|
Duration("waitDelay", &waitDelay, defaultOptions.WaitDelay).
|
||||||
String("waitWindowStatus", &waitWindowStatus, defaultOptions.WaitWindowStatus).
|
String("waitWindowStatus", &waitWindowStatus, defaultOptions.WaitWindowStatus).
|
||||||
@@ -91,6 +93,7 @@ func FormDataChromiumPdfOptions(ctx *api.Context) (*api.FormData, Options) {
|
|||||||
Bool("preferCssPageSize", &preferCssPageSize, defaultOptions.PreferCssPageSize)
|
Bool("preferCssPageSize", &preferCssPageSize, defaultOptions.PreferCssPageSize)
|
||||||
|
|
||||||
options := Options{
|
options := Options{
|
||||||
|
SkipNetworkIdleEvent: skipNetworkIdleEvent,
|
||||||
FailOnConsoleExceptions: failOnConsoleExceptions,
|
FailOnConsoleExceptions: failOnConsoleExceptions,
|
||||||
WaitDelay: waitDelay,
|
WaitDelay: waitDelay,
|
||||||
WaitWindowStatus: waitWindowStatus,
|
WaitWindowStatus: waitWindowStatus,
|
||||||
|
|||||||
@@ -136,7 +136,7 @@ func extraHttpHeadersActionFunc(logger *zap.Logger, extraHttpHeaders map[string]
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func navigateActionFunc(logger *zap.Logger, url string) chromedp.ActionFunc {
|
func navigateActionFunc(logger *zap.Logger, url string, skipNetworkIdleEvent bool) chromedp.ActionFunc {
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
logger.Debug(fmt.Sprintf("navigate to '%s'", url))
|
logger.Debug(fmt.Sprintf("navigate to '%s'", url))
|
||||||
|
|
||||||
@@ -145,12 +145,21 @@ func navigateActionFunc(logger *zap.Logger, url string) chromedp.ActionFunc {
|
|||||||
return fmt.Errorf("navigate to '%s': %w", url, err)
|
return fmt.Errorf("navigate to '%s': %w", url, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = runBatch(
|
waitFunc := []func() error{
|
||||||
ctx,
|
|
||||||
waitForEventDomContentEventFired(ctx, logger),
|
waitForEventDomContentEventFired(ctx, logger),
|
||||||
waitForEventLoadEventFired(ctx, logger),
|
waitForEventLoadEventFired(ctx, logger),
|
||||||
waitForEventNetworkIdle(ctx, logger),
|
|
||||||
waitForEventLoadingFinished(ctx, logger),
|
waitForEventLoadingFinished(ctx, logger),
|
||||||
|
}
|
||||||
|
|
||||||
|
if !skipNetworkIdleEvent {
|
||||||
|
waitFunc = append(waitFunc, waitForEventNetworkIdle(ctx, logger))
|
||||||
|
} else {
|
||||||
|
logger.Debug("skipping network idle event")
|
||||||
|
}
|
||||||
|
|
||||||
|
err = runBatch(
|
||||||
|
ctx,
|
||||||
|
waitFunc...,
|
||||||
)
|
)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user