@Override
    public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
        ResponseWriter writer = context.getResponseWriter();
        PrimeRequestContext requestContext = PrimeRequestContext.getCurrentInstance(context);
        PrimeApplicationContext applicationContext = requestContext.getApplicationContext();
        PrimeConfiguration configuration = applicationContext.getConfig();

        writer.startElement("head", component);
        writer.writeAttribute("id", component.getClientId(context), "id");

        //First facet
        UIComponent first = component.getFacet("first");
        if (FacetUtils.shouldRenderFacet(first)) {
            first.encodeAll(context);
        }

        //Theme
        String theme;
        String themeParamValue = applicationContext.getConfig().getTheme();

        if (themeParamValue != null) {
            ELContext elContext = context.getELContext();
            ExpressionFactory expressionFactory = context.getApplication().getExpressionFactory();
            ValueExpression ve = expressionFactory.createValueExpression(elContext, themeParamValue, String.class);

            theme = ve.getValue(elContext);
        }
        else {
            theme = "saga-blue";     //default
        }

        if (theme != null && !"none".equals(theme)) {
            if (THEME_MAPPING.containsKey(theme)) {
                theme = THEME_MAPPING.get(theme);
            }

            encodeCSS(context, LIBRARY + "-" + theme, "theme.css");
        }

        //Icons
        if (applicationContext.getConfig().isPrimeIconsEnabled()) {
            encodeCSS(context, LIBRARY, "primeicons/primeicons.css");
        }

        //Middle facet
        UIComponent middle = component.getFacet("middle");
        if (FacetUtils.shouldRenderFacet(middle)) {
            middle.encodeAll(context);
        }

        // normal CSV is a required dependency for some special components like fileupload
        encodeValidationResources(context, configuration);

        // encode client side locale
        encodeLocaleResources(context, configuration);

        //Registered Resources
        UIViewRoot viewRoot = context.getViewRoot();
        List<UIComponent> resources = new ArrayList<>(viewRoot.getComponentResources(context, "head"));
        moveResourceToTop(resources, "primeicons/primeicons.css");
        moveResourceToTop(resources, "theme.css");
        for (int i = 0; i < resources.size(); i++) {
            UIComponent resource = resources.get(i);
            LOGGER.log(Level.FINE, () -> "HeadRenderer resource: " + resource.getAttributes().get("name"));
            resource.encodeAll(context);
        }

        encodeSettingScripts(context, requestContext, configuration, writer);

        // encode initialization scripts
        encodeInitScripts(context, requestContext, configuration, writer);
    }
Renders head content based on the following order
- First Facet
- Theme CSS
- PrimeIcons CSS
- Middle Facet
- Registered Resources
- Client Validation Scripts
- Locales
- PF Client Side Settings
- PF Initialization Scripts
- Head Content
- Last Facet
primefaces/src/main/java/org/primefaces/renderkit/HeadRenderer.java