• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rtCamp / snapwp / 14464649106

15 Apr 2025 08:17AM UTC coverage: 53.748% (+0.1%) from 53.621%
14464649106

Pull #151

github

web-flow
Merge 680b91de4 into 278ef2cd9
Pull Request #151: fix: Add `is404` flag for 404ed routes

188 of 433 branches covered (43.42%)

Branch coverage included in aggregate %.

1 of 2 new or added lines in 2 files covered. (50.0%)

4 existing lines in 1 file now uncovered.

400 of 661 relevant lines covered (60.51%)

6.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/packages/next/src/template-renderer/index.tsx
1
import { headers } from 'next/headers';
2
import Script from 'next/script';
3
import { QueryEngine } from '@snapwp/query';
4
import { TemplateHead } from './template-head';
5
import { TemplateScripts } from './template-scripts';
6

7
import type { BlockData } from '@snapwp/types';
8
import type { ReactNode } from 'react';
9

10
export type TemplateRendererProps = {
11
        getTemplateData?: ( typeof QueryEngine )[ 'getTemplateData' ];
12
        children: ( args: {
13
                editorBlocks: BlockData[];
14
                is404: boolean;
15
        } ) => ReactNode;
16
};
17

18
/**
19
 * Renders a full HTML document including the head, body, and scripts.
20
 * Combines custom styles, block content, and scripts into a complete page.
21
 *
22
 * @param {Object}                                   props                 The props for the component.
23
 * @param {TemplateRendererProps['getTemplateData']} props.getTemplateData A async callback to get template styles and content.
24
 * @param {TemplateRendererProps['children']}        props.children        The block content to render.
25
 *
26
 * @return A complete HTML document structure.
27
 */
28
export async function TemplateRenderer( {
29
        getTemplateData = QueryEngine.getTemplateData,
×
30
        children,
31
}: TemplateRendererProps ): Promise< ReactNode > {
UNCOV
32
        const headerList = await headers(); // headers() returns a Promise from NextJS 19.
×
33
        const pathname = headerList.get( 'x-current-path' );
×
34

35
        const {
36
                editorBlocks,
37
                bodyClasses,
38
                stylesheets,
39
                scripts,
40
                scriptModules,
41
                is404,
NEW
42
        } = await getTemplateData( pathname || '/' );
×
43

UNCOV
44
        if ( ! editorBlocks?.length ) {
×
UNCOV
45
                throw new Error(
×
46
                        'Error: Unable to render content. `editorBlocks` is not defined. This may be due to missing template data or an issue with data fetching.'
47
                );
48
        }
49

50
        // @todo: Script modules should load before styles to handle ordering properly
UNCOV
51
        return (
×
52
                <>
53
                        <TemplateHead stylesheets={ stylesheets } />
54
                        <TemplateScripts
55
                                scripts={ scripts }
56
                                scriptModules={ scriptModules }
57
                        >
58
                                <main>
59
                                        <div className="wp-site-blocks">
60
                                                { children( { editorBlocks, is404 } ) }
61
                                        </div>
62
                                </main>
63
                        </TemplateScripts>
64
                        { /* Hot Fix for adding classes to the body outside the root layout */ }
65

66
                        <Script
67
                                strategy="beforeInteractive"
68
                                dangerouslySetInnerHTML={ {
69
                                        __html: `
70
                                                        ${ JSON.stringify( bodyClasses ) }.forEach( ( c ) => {
71
                                                                document.body.classList.add( c );
72
                                                        } );
73
                                                `,
74
                                } }
75
                        />
76
                </>
77
        );
78
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc