Skip to content

Commit

Permalink
Prevent crawlers from indexing glitched content
Browse files Browse the repository at this point in the history
  • Loading branch information
7PH committed Oct 23, 2024
1 parent 618bba4 commit 9ee1871
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ export type PlayModes = 'always' | 'hover' | 'click' | 'manual';
* Custom options for the glitch animations.
*/
export type PowerGlitchOptions = {
/**
* Whether to avoid running the glitch effect on crawlers for SEO optimization.
*/
optimizeSeo: boolean,

/**
* Html to glitch. If not provided, will use the elements themselves. If provided, all elements should have an `innerHTML` property.
*/
Expand Down Expand Up @@ -170,6 +175,7 @@ export type LayerDefinition = {
const getDefaultOptions = (playMode: PlayModes = 'always'): PowerGlitchOptions => {
return {
playMode,
optimizeSeo: true,
createContainers: true,
hideOverflow: false,
timing: playMode === 'always' ? { duration: 2 * 1000, iterations: Infinity } : { duration: 250, iterations: 1 },
Expand Down Expand Up @@ -545,10 +551,15 @@ export type GlitchResult = {
* @param elOrSelector What to glitch. Can be a query selector, a list of HTMLElement, an HTMLElement or a NodeList.
* @param userOptions Optional glitch customization options.
*/
const glitch = (elOrSelector: GlitchableElement = '.powerglitch', userOptions: GlitchPartialOptions = {}): GlitchResult => {
const glitch = (elOrSelector: GlitchableElement = '.powerglitch', userOptions: GlitchPartialOptions = {}): GlitchResult | null => {
// Fix options with defaults
const options: PowerGlitchOptions = mergeOptions(getDefaultOptions(userOptions.playMode), userOptions);

// Do NOT glitch if SEO optimization is enabled and the user agent is a bot
if (options.optimizeSeo && navigator.userAgent.match(/bot|google|baidu|bing|msn|teoma|slurp|yandex|facebookexternalhit|facebot/i)) {
return null;
}

// Find elements to glitch
let elements: HTMLElement[] = [];
if (typeof elOrSelector === 'string') {
Expand Down

0 comments on commit 9ee1871

Please sign in to comment.