PurgeCSSPurgeCSS
API Reference
  • English
  • Français
GitHub
API Reference
  • English
  • Français
GitHub
  • PurgeCSS

    • About PurgeCSS
    • Getting Started
    • Configuration
    • Command Line Interface
    • Programmatic API
    • Safelisting
    • Extractors
  • Plugins

    • PostCSS
    • Webpack
    • Rollup
    • Gulp
    • Grunt
    • Gatsby
  • Guides

    • Vue
    • React
    • Angular
    • Next.js
    • Nuxt.js
    • Razzle
    • WordPress
    • Hugo
  • Comparison
  • Common Questions

    • How to use with CSS modules
    • How to use with Ant Design

Home > purgecss > ContentCache

ContentCache class

Cache of the selectors extracted from content files, shared across PurgeCSS instances.

Integrations that run PurgeCSS many times with the same content, e.g. a PostCSS plugin called once per CSS file or a bundler plugin in watch mode, can set the same cache on every PurgeCSS instance they create with PurgeCSS.contentCache. A content file is then only read and extracted again when its modification time, change time, size, inode or the extractor used for it changed. Runs at the same time share the files being read and extracted: each file is extracted once.

A file modified or changed (e.g. its modification time set back) less than 2 seconds before it was read is extracted again on the next run, as a modification within the precision of the file system timestamps would keep the same modification and change times.

The paths given to the methods are resolved from the current working directory.

Globs are still resolved on every run, so new files are picked up and deleted files no longer contribute selectors. The entries of the files that are no longer used stay in the cache until ContentCache.prune(), ContentCache.delete() or ContentCache.clear() is called.

The extractors are called concurrently and in no particular order: they must not depend on the order of the files or on a state kept between files.

Signature:

export declare class ContentCache 

Example

const contentCache = new ContentCache()
for (const css of cssFiles) {
  const purgeCSS = new PurgeCSS()
  purgeCSS.contentCache = contentCache
  await purgeCSS.purge({ content: ['src/pages/*.html'], css: [css] })
}

Properties

Property

Modifiers

Type

Description

size

readonly

number

Number of files in the cache

Methods

Method

Modifiers

Description

clear()

Remove all the files from the cache

delete(filePath)

Remove a file from the cache

get(filePath, stats, extractor)

Get the selectors of a file if they are still valid. The selectors may still be being extracted by another run.

has(filePath)

Check if a file is in the cache, whether its entry is still valid or not

keys()

Absolute paths of the files in the cache

prune(filePaths)

Remove the files that are not in the given list, e.g. the files of a previous build that the content globs no longer match

set(filePath, entry)

Store the selectors of a file. The entry is removed when its selectors reject, or once they resolve when the file was modified or changed less than 2 seconds before ContentCacheEntry.checkedAt.