Whitespace (TABulators
, NewLines
, and SPaces
) are characters in e.g. HTML pages which don't have syntactic significance but are just supposed to ease the reading by humans.
Whether you put one space between </p>
and the following <p>
doesn't make a difference as far as parsing and rendering the HTML page is concerned – it just increases the filesize and therefore the amount of bandwidth needed, the time used for transfer and interpretation, the amount of memory used in both the sending server and the receiving user/browser.
In the end one could say: the more whitespace there is in your HTML pages the more expensive it is for all parties involved.
When writing Nele and Kaliber (both of which are essentially web-servers) I realised that I basically implemented the same code for removing superfluous whitespace before delivering the HTML pages to the remote user. So I extracted that code and refactored it to become the simple middleware plugin you can see here.
You can use Go
to install this package for you:
go get -u github.com/mwat56/whitespace
There are two ways to use this library:
- As a middleware plugin which then will be used automatically.
- By calling the
Remove()
function from your code whenever it suits you.
In both cases you can use this library by
import "github.com/mwat56/whitespace"
To use it as middleware for your web-server you call
whitespace.Wrap(aHandler http.Handler) http.Handler
where aHandler
is the page-handler you implemented to handle (generate and send) your web-pages.
The function's return value can then be used to set up your http.Server
instance.
If instead you want to use the library manually you can call
whitespace.Remove(aPage []byte) []byte
where aPage
is the HTML page you prepared and the function's return value is that very page with all superfluous whitespace removed.
You can de-/activate the removal behaviour at runtime by setting the boolean UseRemoveWhitespace
flag.
If it is true
(i.e. the default) both the Remove()
and Wrap()
functions work as expected (see above).
However, if you set that flag to false
no whitespace removal takes place and the HTML pages produced by your server will be send to the remote user as they were generated (including the whitespace you and/or your tools put there).
No external libraries were used building whitespace
.
Copyright © 2020, 2024 M.Watermann, 10247 Berlin, Germany
All rights reserved
EMail : <[email protected]>
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
You should have received a copy of the GNU General Public License along with this program. If not, see the GNU General Public License for details.