Home / Function/ New() — fiber Function Reference

New() — fiber Function Reference

Architecture documentation for the New() function in static.go from the fiber codebase.

Entity Profile

Dependency Diagram

graph TD
  d98bccf4_de44_c71f_4e71_9c0ad58f76bb["New()"]
  3c44fb0a_14a9_26fb_2c78_c413f4b9d39d["static.go"]
  d98bccf4_de44_c71f_4e71_9c0ad58f76bb -->|defined in| 3c44fb0a_14a9_26fb_2c78_c413f4b9d39d
  5d2693ce_0aae_4447_b8ad_2e2dcb4f96c7["isFile()"]
  d98bccf4_de44_c71f_4e71_9c0ad58f76bb -->|calls| 5d2693ce_0aae_4447_b8ad_2e2dcb4f96c7
  4ab56980_491f_71ac_10f1_6d1ef45c4234["sanitizePath()"]
  d98bccf4_de44_c71f_4e71_9c0ad58f76bb -->|calls| 4ab56980_491f_71ac_10f1_6d1ef45c4234
  style d98bccf4_de44_c71f_4e71_9c0ad58f76bb fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

middleware/static/static.go lines 121–267

func New(root string, cfg ...Config) fiber.Handler {
	config := configDefault(cfg...)

	var createFS sync.Once
	var fileHandler fasthttp.RequestHandler
	var cacheControlValue string
	var rootIsFile bool

	// adjustments for io/fs compatibility
	if config.FS != nil && root == "" {
		root = "."
	}

	return func(c fiber.Ctx) error {
		// Don't execute middleware if Next returns true
		if config.Next != nil && config.Next(c) {
			return c.Next()
		}

		// We only serve static assets on GET or HEAD methods
		method := c.Method()
		if method != fiber.MethodGet && method != fiber.MethodHead {
			return c.Next()
		}

		// Initialize FS
		createFS.Do(func() {
			prefix := c.Route().Path

			if check, err := isFile(root, config.FS); err == nil {
				rootIsFile = check
			}

			// Is prefix a partial wildcard?
			if before, _, found := strings.Cut(prefix, "*"); found {
				// /john* -> /john
				prefix = before
			}

			prefixLen := len(prefix)
			if prefixLen > 1 && prefix[prefixLen-1:] == "/" {
				// /john/ -> /john
				prefixLen--
			}

			fileServer := &fasthttp.FS{
				Root:                   root,
				FS:                     config.FS,
				AllowEmptyRoot:         true,
				GenerateIndexPages:     config.Browse,
				AcceptByteRange:        config.ByteRange,
				Compress:               config.Compress,
				CompressBrotli:         config.Compress, // Brotli compression won't work without this
				CompressZstd:           config.Compress, // Zstd compression won't work without this
				CompressedFileSuffixes: c.App().Config().CompressedFileSuffixes,
				CacheDuration:          config.CacheDuration,
				SkipCache:              config.CacheDuration < 0,
				IndexNames:             config.IndexNames,
				PathNotFound: func(fctx *fasthttp.RequestCtx) {
					fctx.Response.SetStatusCode(fiber.StatusNotFound)
				},
			}

			fileServer.PathRewrite = func(fctx *fasthttp.RequestCtx) []byte {
				path := fctx.Path()

				if len(path) >= prefixLen {
					checkFile, err := isFile(root, fileServer.FS)
					if err != nil {
						return path
					}

					// If the root is a file, we need to reset the path to "/" always.
					switch {
					case checkFile && fileServer.FS == nil:
						path = []byte("/")
					case checkFile && fileServer.FS != nil:
						path = utils.UnsafeBytes(root)
					default:
						path = path[prefixLen:]
						if len(path) == 0 || path[len(path)-1] != '/' {

Domain

Subdomains

Frequently Asked Questions

What does New() do?
New() is a function in the fiber codebase, defined in middleware/static/static.go.
Where is New() defined?
New() is defined in middleware/static/static.go at line 121.
What does New() call?
New() calls 2 function(s): isFile, sanitizePath.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free