Working with Pages
Fetch, cache, edit, and inspect wiki pages through the PageManager and Page structure.
Fetching a page
const page = await client.pages.fetch("Sonic_the_Hedgehog");
console.log(page.extract);- The manager calls
action=querywithprop=extracts|revisions|categoriesand automatically follows redirects so you always land on the canonical title. - Responses are cached inside an
LRUCachekeyed bypage:<title>. Adjustclient.options.cacheSizeto change how many entries are stored. - When MediaWiki does not return an extract, fandom.js falls back to
Articles/Detailsand requests an abstract (DEFAULT_ABSTRACT_LENGTHcharacters) so you still get a summary.
The cache lives on the client instance. Reuse a single Client per process to
avoid duplicate fetches.
Page structure anatomy
Page extends BaseStructure and patches data as more information becomes available.
| Field | Source |
|---|---|
pageid, ns, title | query.pages[].* |
extract | extracts prop or Articles/Details fallback |
content | First revision slot content |
categories | Array of category titles |
console.log(page.categories);
// ['Category:Characters', 'Category:Articles needing cleanup']Editing content
await page.edit(
`New content at ${new Date().toISOString()}`,
"Refresh lead paragraph",
);Page.editissuesaction=editand automatically requests a CSRF token.- Provide an edit summary to leave a paper trail inside MediaWiki.
Deleting and protecting pages
await page.delete("Spam page detected");
await page.protect(
ProtectionLevel.Autoconfirmed,
"1 week",
"Lock to autoconfirmed users",
);- Protection applies to both edit and move rights via
protections=edit=<level>|move=<level>. - Expiry strings follow MediaWiki syntax (e.g.,
infinite,2024-12-01T00:00:00Z).
Working with history
const history = await page.fetchHistory();
console.log(history[0].user, history[0].timestamp);
await page.revertTo(history.at(-1)!.revid, "Restore original text");fetchHistoryrequests up to 50 revisions and returnsRevisionobjects so you can inspect content, user, timestamp, and comments.revertTocompares the current revision ID to the one you pass. When they differ it issues an undo edit withundo/undoafterparams.
Emitting page events
Every successful fetch triggers client.events.emit('pageFetched', pageSummary). Listen to it when you need to
attach logging or analytics without modifying your application code.
How is this guide?